BeanFactory中Bean的生命周期:
package com.Spring.BeanLifeCycle;
...
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("调用Car的无参构造函数");
}
public void setBrand(String brand) {
System.out.println("调用setBrand()方法设置属性");
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void setMaxSpeed(int maxSpeed){
this.maxSpeed = maxSpeed;
}
public int getMaxSpeed(){
return maxSpeed;
}
public void introduce() {
System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:"
+ maxSpeed);
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("调用BeanFactoryAware.setBeanFactory()。");
this.beanFactory = beanFactory;
}
public void setBeanName(String beanName) {
System.out.println("调用BeanNameAware.setBeanName()。");
this.beanName = beanName;
}
public void afterPropertiesSet() throws Exception {
System.out.println("调用InitializingBean.afterPropertiesSet()。");
}
public void destroy() throws Exception {
System.out.println("调用DisposableBean.destory()。");
}
public void myInit(){
System.out.println("调用init-method所指定的myInit(),将maxSpeed设置为240");
this.maxSpeed = 240;
}
public void myDistroy(){
System.out.println("调用destory-method所指定的myDestroy()。");
}
}
package com.Spring.BeanLifeCycle;
...
public class MyInstantiationAwareBeanPostProcessor extends
InstantiationAwareBeanPostProcessorAdapter {
public boolean postProcessAfterInstantiation(Object bean, String beanName)
throws BeansException {
if ("car".equals(beanName)) {
System.out
.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
}
return true;
}
public Object postProcessBeforeInstantiation(Class beanClass,
String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out
.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
}
return null;
}
public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
if ("car".equals(beanName)) {
System.out
.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");
}
return pvs;
}
}
package com.Spring.BeanLifeCycle;
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (beanName.equals("car")) {
Car car = (Car) bean;
if (car.getMaxSpeed() >= 200) {
System.out.println("调用BeanPostProcessor.postProcess After Initialization(),将maxSpeed调整为200");
car.setMaxSpeed(200);
}
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (beanName.equals("car")) {
Car car = (Car) bean;
if (car.getColor()==null) {
System.out.println("调用BeanPostProcessor.postProcess Before Initialization(),将color设置为默认黑色");
car.setColor("黑色");
}
}
return bean;
}
}
package com.Spring.BeanLifeCycle;
public class BeanLifeCycle {
public static void main(String[] args) {
lifeClcleInBeanFactory();
}
private static void lifeClcleInBeanFactory() {
// 装载配置文件并启动容器
Resource res = new ClassPathResource("com/Spring/BeanLifeCycle/beans.xml");
BeanFactory bf = new XmlBeanFactory(res);
// 向容器中注册BeanPostProcessor后处理器
((ConfigurableBeanFactory) bf)
.addBeanPostProcessor(new MyBeanPostProcessor());
// 向容器中注册后处理器InstantiationAwareBeanPostProcessor后处理器
((ConfigurableBeanFactory) bf)
.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
Car car1 = (Car) bf.getBean("car");// 第一次从容器中获取car,将触发容器实例化该Bean
car1.introduce();
car1.setColor("红色");
Car car2 = (Car) bf.getBean("car");// 第二次从容器中获取car,直接从缓存池中获得
System.out.println("car1==car2:" + (car1 == car2));
((XmlBeanFactory)bf).destroySingletons();//关闭容器
}
Bean完整的生命周期从Spring容器的着手实例化Bean 开始,知道最终销毁Bean。这当中经过了许多的关键点,每个关键点都涉及特定的方法调用,可以将这些方法大致划分为三类:
·Bean自身的方法:如调用Bean构造函数实例化Bean,调用Setter设置Bean的属性以及通过<bean>的init-method 和 destroy-method所指定的方法;
·Bean级生命周期接口方法:如BeanNameAware,BeanFactoryAware,InitializingBean和DisposableBean,这些接口由Bean直接实现;
·容器级生命周期接口方法:上例用InstantiationAwareBeanPostProcessorAdapter和BeanPostProcessor这两个接口实现,一般称他们的实现类为后处理器。一般不由Bean直接实现,它们独立于Bean,实现类以容器附加装置的形式注册到Spring容器中。当Spring创建任何的Bean时,后处理器都会发生作用,所以这些后处理器是全局性的。
具体的生命周期即从着手实例化开始到销毁的具体步骤:
1.如果注册了InstantiationAwareBeanPostProcessor后处理器,执行InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()方法;
2.根据配置情况调用Bean构造函数或工厂方法实例化Bean;
3.如果注册了InstantiationAwareBeanPostProcessor后处理器,执行InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation()方法;
4.如果<bean>配置了<property>属性,在设置每个属性前调用InstantiationAwareBeanPostProcessor.postProcessPropertyValues()方法;
5.调用Bean的属性设置方法setXxx()方法设置属性值;
6.如果Bean实现了BeanNameAware.setBeanName()方法,将配置文件中该Bean对应的名称设置到Bean中通过setBeanName()方法;
7.如果Bean实现了BeanFactoryAware.setBeanFactory()方法,将调用setBeanFactory()接口的方法,将BeanFactory容器实例设置到Bean中;
8.调用BeanPostProcessor.postProcessBeforeInitialization(),入参是当前正在处理的Bean,beanName是当前Bean的配置名,返回的对象为加工处理后的Bean,可以在此方法中对Bean进行修改;
9.如果Bean实现了调用InitializingBean,调用InitializingBean.afterPropertiesSet()方法;
10.执行在<bean>通过init-method属性定义的初始化方法;
11.调用BeanPostProcessor.postProcessAfterInitialization()方法;
12.如果在<bean>中指定Bean的作用范围为scope="prototype",将Bean返回给调用者,调用者负责Bean后续生命管理,Spring不再管理这些Bean的生命周期。如果为Scope="singleton",则将Bean放入Spring IoC容器的缓存池,并将Bean引用返回给调用者,Spring继续对这些Bean进行后续的生命管理。
13.对于scope="singleton"的Bean,当容器关闭时,调用DisposableBean.destory()方法;
14.调用<bean>中destory-method方法。
ApplicationContext中Bean的生命周期:
在应用上下文中,Bean 的生命周期和在BeanFactory类似,只是如果Bean实现了org.springframework.context.ApplicationContextAware接口,会在BeanFactoryAware的setBeanFactory()方法后调用ApplicationContextAware接口的setApplicationContext()方法。
他们另外一个最大不同在于Application会利用java反射机制自动判断出配置文件中定义的BeanPostProcessor,InstantiationAwareBeanPostProcessor和BeanFactoryPostProcessor,并将它们自动注册到应用上下文中;而BeanFactory需要在代码中手工调用addBeanPostProcessor()方法进行注册。所以在开发中使用ApplicationContext而很少使用BeanFactory的原因。
ApplicationContext在启动时,将首先为配置文件中每一个<bean>生成一个BeanDefinition对象,BeanDefinition是<bean>在Spring容器中的内部表示。当配置文件中所有的<bean>都被解析成BeanDefinition时,ApplicationContext将调用工厂后处理器的方法,从而有机会通过程序的方式调整Bean的配置信息。