Article Directory
Bean's life cycle
1. Concept
- In a nutshell, the life cycle of a bean is the process from object creation to object destruction.
Second, the life cycle of the bean
(1) Create a bean instance through the constructor ( 无参数构造
)
(2) Set values for bean properties and reference to other beans ( 调用 set 方法
)
(3) Call the method of bean initialization ( 进行配置初始化的方法
)
(4) The bean can be used ( 对象获取
)
(5) When the container is closed, call the method of destroying the bean ( 进行配置销毁的方法
)
Three, demonstrate the life cycle of the bean
Ⅰ. Create the Orders class
- Declare attributes
- Write a parameterless constructor
- Write the set() method
- Create initialization method
- Create bean destruction method
public class Orders {
// 声明属性
private String oName;
// 1.无参构造
public Orders() {
System.out.println("第一步:执行无参构造器创建bean实例");
}
// 2.set
public void setoName(String oName) {
this.oName = oName;
System.out.println("第二步:调用set方法设置属性的值");
}
// 3.创建执行的初始化的方法 --- 在xml文件中配置
public void initMethod() {
System.out.println("第三步:执行初始化的方法");
}
// 5.创建执行的bean销毁方法
public void destroyMethod() {
System.out.println("第五步:执行销毁方法");
}
}
Ⅱ. Modify the configuration file
- Configure to create a bean object
- Add
init-method属性
, specify the initialization method - Add
destroy-method属性
, specify the method of destroying the bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean对象 -->
<bean id="oders" class="Bean的生命周期.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oName" value="电脑"></property>
</bean>
</beans>
Ⅲ. Writing test classes
public class Test {
@org.junit.Test
public void test_orders(){
try {
ApplicationContext context = new ClassPathXmlApplicationContext("Bean的生命周期/bean_Orders.xml");
// 4.获取bean实例对象
Orders orders = context.getBean("orders",Orders.class);
System.out.println("第四步:获取创建的bean实例对象");
// 5.手动销毁bean实例对象
((ClassPathXmlApplicationContext)context).close();
} catch (Exception e){
e.getMessage();
}
}
}
4. Bean's post processor, the bean life cycle has seven steps
(1) Create a bean instance through the constructor ( 无参数构造
)
(2) Set values for bean properties and reference to other beans ( 调用 set 方法
)
- The method of passing the bean instance to the bean post processor
postProcessBeforeInitialization
(3) Call the method of bean initialization ( 进行配置初始化的方法
)
- The method of passing the bean instance to the bean post processor
postProcessAfterInitialization
(4) The bean can be used ( 对象获取
)
(5) When the container is closed, call the method of destroying the bean ( 进行配置销毁的方法
)
▶ Demonstrate the effect of adding a post processor
(1) Create a class, implement the interface BeanPostProcessor, and create a post processor
public class MyBeanPost implements BeanPostProcessor {
/**
* 初始化之前调用
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
/**
* 初始化之后调用
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
(2) Modify the configuration file
- You only need to configure the bean object of the post processor class, becauseThe post-processor class implements the
BeanPostProcessor接口
post-processor, then the post-processor will add post-processor processing to all beans in the current configuration file。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean对象 -->
<bean id="orders" class="Bean的生命周期.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oName" value="电脑"></property>
</bean>
<!-- 配置后置处理器 -->
<bean id="myBeanPost" class="Bean的生命周期.Test.MyBeanPost"></bean>
</beans>