一:IOC容器中Bean的生命周期方法
1.spring IOC容器中可以管理Bean的生命周期,spring允许在Bean的生命周期的特定点执行定制的任务。
2.spring IOC容器对Bean的生命周期进行管理的过程:
- 通过构造器或者工厂方法创建Bean实例
- 为Bean的属性设置值和对其他Bean的引用
- 调用Bean的初始化方法
- Bean可以使用了
- 当容器关闭时,调用Bean的销毁方法
3.在Bean的声明里设置init-method和destroy-method属性,为Bean指定初始化和销毁方法。
二:代码实现
1.创建一个Car类
package com.cycle.dhx;
public class Car {
private String name;
public Car() {
System.out.println("Car construstor...");
}
public void setName(String name) {
System.out.println("setName...");
this.name = name;
}
public void init() {
System.out.println("init...");
}
public void destory() {
System.out.println("destory...");
}
}
2.在配置文件里配置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">
<!--其中init-method和 destroy-method方法名不一定非要起 init和destory,见名知义即可-->
<bean id="car" class="com.cycle.dhx.Car"
init-method="init"
destroy-method="destory">
<property name="name" value="baoma"></property>
</bean>
</beans>
3.获取Bean实例
package com.cycle.dhx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//因为这里用ApplicationContext没有提供关闭容器的方法,在这里我们使用ClassPathXmlApplicationContext
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car=(Car) ctx.getBean("car");
System.out.println(car);
//关闭IOC容器
ctx.close();
}
}
三:创建Bean后置处理器
1.Bean后置处理器允许在调用初始化前后对Bean进行额外的处理。
2.Bean后置处理器对IOC容器所有的Bean实例逐一处理而非单一的实例,其典型应用是:检查Bean属性的正确性或根据特定的标志更改Bean的属性。
3.对Bean后置处理器而言,需要实现BeanPostProcessor接口,在初始化方法被调用前后,spring将每个Bean实例分别传递给上述接口的以下两个接口方法(postProcessAfterInitialization()和postProcessBeforeInitialization())。
四:创建Bean后置处理器后Bean的生命周期
1.spring IOC容器对Bean的生命周期进行管理的过程
- 通过构造器或者工厂方法创建Bean
- 为Bean属性设置值和对其他Bean的引用
- 将Bean的实例传递给Bean后置处理器的postProcessBeforeInitialization方法
- 调用Bean的初始化方法
- 将Bean的实例传递给Bean后置处理器的postProcessAfterInitialization方法
- Bean可以使用了
- 当容器关闭时,调用Bean的销毁方法
五:代码实现
1.创建一个MyBeanProcessor 类,并实现BeanPostProcessor接口
package com.cycle.dhx;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization:"+bean+","+beanName);
//因为bean后置处理器是对所有的Bean操作,我们可以在这过滤一下
if("car".equals(beanName)) {
//....
}
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization:"+bean+","+beanName);
//bean后置处理器很强大,我们可以重新修改bean的属性
Car car=new Car();
car.setName("laosilaisi");
return car;
}
}
2.配置xml文件
<?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">
<!--其中init-method和 destroy-method方法名不一定非要起 init和destory,见名知义即可-->
<bean id="car" class="com.cycle.dhx.Car"
init-method="init"
destroy-method="destory">
<property name="name" value="baoma"></property>
</bean>
<!--
实现BeanPostProcessor接口,并具体提供
Object postProcessAfterInitialization(Object bean, String beanName):init-method方法,之后被调用
Object postProcessBeforeInitialization(Object bean, String beanName):init-method方法,之前被调用
bean:bean实例本身
beanName:IOC容器配置的bean的名字
返回值:是实际上返回给用户的那个bean,注意:可以在以上两个方法中修改返回的bean,甚至返回一个新的bean
-->
<!--配置bean的后置处理器,不需要配置id,IOC容器自动识别是一个 BeanPostProcessor-->
<bean class="com.cycle.dhx.MyBeanProcessor"></bean>
</beans>