public class BeanLifeCycle {
public static void main(String[] args) {
Resource resource = new ClassPathResource("application-context/spring/beans.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new MyBeanPostProcessor());
((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new InitDestroyAnnotationBeanPostProcessor());
Car car1 = (Car) beanFactory.getBean("car");
System.out.println(car1.toString());
car1.setColor("red");
System.out.println("****************************************************************");
Car car2 = (Car) beanFactory.getBean("car");
System.out.println(car2.toString());
System.out.println("car1 == car2: " + (car1 == car2));
((XmlBeanFactory)beanFactory).destroySingletons();
}
}
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
}
return super.postProcessBeforeInstantiation(beanClass, beanName);
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
}
return super.postProcessAfterInstantiation(bean, beanName);
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessPropertyValues");
}
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object object, String beanName) throws BeansException {
if ("car".equals(beanName)) {
Car car = (Car) object;
if (car.getColor() == null) {
System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization. set Color black");
car.setColor("black");
}
}
return object;
}
@Override
public Object postProcessAfterInitialization(Object object, String beanName) throws BeansException {
if ("car".equals(beanName)) {
Car car = (Car) object;
if (car.getMaxSpeed() > 200) {
System.out.println("MyBeanPostProcessor.postProcessAfterInitialization, set MaxSpeed to 200.");
car.setMaxSpeed(200);
}
}
return object;
}
}
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
BeanDefinition db = configurableListableBeanFactory.getBeanDefinition("car");
db.getPropertyValues().addPropertyValue("brand", "qiruiQQ");
System.out.println("MyBeanFactoryPostProcessor.postProcessBeanFactory");
}
}
public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
private String brand;
private String color;
private int maxSpeed;
private BeanFactory beanFactory;
private String beanName;
public Car() {
System.out.println("call constructor");
}
public Car(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware setBeanFactory");
this.beanFactory = beanFactory;
}
@Override
public void setBeanName(String s) {
System.out.println("BeanNameAware setBeanName");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean afterPropertiesSet");
}
public void setBrand(String brand) {
System.out.println("call setBrand");
this.brand = brand;
}
public void setColor(String color) {
System.out.println("call setColor");
this.color = color;
}
public void setMaxSpeed(int maxSpeed) {
System.out.println("call setMaxSpeed");
this.maxSpeed = maxSpeed;
}
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void myInit() {
System.out.println("init-method.");
this.maxSpeed = 240;
}
public void myDestroy() {
System.out.println("destroy-method.");
}
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct");
}
@PreDestroy
public void preDestroy() {
System.out.println("preDestroy");
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", maxSpeed=" + maxSpeed +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd ">
<bean id="car" class="org.anicloud.spring4.spring.lifecycle.Car"
p:brand="bwm"
p:maxSpeed="200"
init-method="myInit"
destroy-method="myDestroy"
scope="singleton"/>
</beans>
分享到:
相关推荐
接下来,我们将深入探讨Spring Bean生命周期的各个阶段,并通过具体的示例来演示。 1. **配置阶段** 在Spring容器初始化时,它会读取配置文件(如XML或Java配置),解析Bean定义,包括其依赖关系、初始化方法、...
- **XML配置**:在传统的Spring应用中,Bean的定义通常写在XML配置文件中,如`springbean-xml`中的配置。 - **注解配置**:使用`@Component`,`@Service`,`@Repository`和`@Controller`注解标记类,配合`@...
其中,Spring Bean生命周期的管理是Spring框架的核心功能之一,它涉及Spring容器如何创建、配置以及销毁Bean的整个过程。理解Spring Bean的生命周期对于开发高效和可维护的Java应用至关重要。 Spring Bean生命周期...
Spring Bean 生命周期是 Spring 框架中的一个核心概念,了解 Spring Bean 的生命周期对我们了解整个 Spring 框架会有很大的帮助。本文将详细介绍 Spring Bean 生命周期的概念、生命周期图、生命周期阶段、生命周期...
Spring框架是Java应用开发中的一个核心组件,尤其在企业级应用中广泛使用。Spring通过其IoC(Inversion of Control,控制反转)容器管理Bean的生命周期,...理解Bean生命周期对于有效管理和优化Spring应用至关重要。
本篇将详细阐述Spring Bean生命周期的各个阶段,以及如何记忆和深入理解这一过程。 首先,Spring Bean的生命周期可以分为两个主要阶段:容器初始化阶段和容器销毁阶段。 在容器初始化阶段,Spring Bean会经历以下...
在Spring框架中,Spring Bean的生命周期始于其创建过程,即"我从哪里来?"的问题。首先,要明确几个核心概念:Java Bean、Spring Bean和Spring IoC(控制反转)容器。Java Bean是一种遵循特定规范的Java类,而Spring...
在Spring框架中,Bean生命周期是核心概念之一,它涉及到Bean的创建、初始化、使用和销毁等阶段。了解和掌握Bean生命周期对于开发高质量的Spring应用至关重要。以下是对Spring Bean生命周期的详细解析。 首先,Bean...
Spring中Bean的生命周期和作用域及实现方式 Spring是一个非常流行的Java应用程序框架,它提供了一个灵活的机制来管理Bean的生命周期和作用域。Bean的生命周期和作用域是Spring框架中两个非常重要的概念,它们决定了...
下面以一个简单的Person类为例,展示Spring Bean生命周期的各个阶段: ```java public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean { // 省略属性和构造器 @...
在Spring框架中,在IOC容器中管理的Bean分为单例和原型两种,单例Bean在容器启动时就实例化,原型Bean则是每次从容器中请求时才会实例化。
本资源提供了在Spring 4.2环境下关于Bean生命周期的测试代码,帮助我们深入理解这一关键概念。 首先,让我们了解Spring Bean的生命周期主要包含以下几个阶段: 1. **实例化**:Spring容器通过`Class`对象或`...
SpringBean的生命周期.mdj
在Spring框架中,Bean的生命周期管理和ApplicationContext的应用是两个核心概念,它们对于理解Spring如何管理和协调应用中的对象至关重要。本文将深入探讨这两个主题,并结合国际化(i18n)和事件传递来阐述它们在...
Bean生命周期的理解对于优化应用性能和理解Spring内部工作原理至关重要。这里我们将深入探讨Spring的Bean生命周期、容器周期以及工厂周期。 首先,让我们从Bean生命周期开始。在Spring中,Bean的生命周期包括以下几...
在Spring框架中,Bean的生命周期管理是其核心功能之一,它允许开发者控制Bean从创建到销毁的整个过程。本文将详细解析Spring Bean的生命周期配置过程。 首先,Bean的生命周期始于实例化。当Spring容器...
Spring中的Bean生命周期主要分为两个阶段:容器管理阶段和用户自定义阶段。在容器管理阶段,Spring容器负责Bean的实例化、属性注入、初始化、使用和销毁。而在用户自定义阶段,开发者可以通过定义回调方法或使用特定...