Bean的生命周期
正确理解Spring bean的生命周期非常重要,因为你或许要利用Spring提供的扩展点来完成特定需求。如下图展示了bean 装载到Spring应用上下文中的一个典型的生命周期过程。
接下来我们对该图中的内容进行详细解释
- Spring对bean进行实例化;
- Spring将值和bean的引用注入到bean对应的属性中;
- 如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBean-Name()方法,然后调用这个方法;
- 如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将 BeanFactory容器实例传入;
- 如果bean实现了ApplicationContextAware接口,Spring将调 用setApplicationContext()方法,将bean所在的应用上下文的引用传入进来;
- 如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization()方法;
- 如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()方法;
- 如果Bean使用init-method,他就会调用已定义的初始化方法;
- 如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization()方法;
- 此时,bean已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该 应用上下文被销毁;
当服务器正常关闭,过着遇到其他关闭Spring IOC容器的事件,他就会调用对应的方法完成Bean的销毁,其步骤如下:
11. 如果bean实现了DisposableBean接口,Spring将调用它的destroy()接口方法。
12. 同样, 如果bean使用destroy-method声明了销毁方法,该方法也会被调用。
Bean生命周期测试实例
如果看不懂上面也没有关系,我们接下来通过一个实例来具体看看bean的生命周期
- Source类,该类提供果汁的描述
public class Source {
private String size;
private String name;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Source() {
}
public Source(String size, String name) {
this.size = size;
this.name = name;
}
}
- BeanPostProcessorImpl类, BeanPostProcessor接口的实现类,你会发现所有bean都会调用该实现。
public class BeanPostProcImpl implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("【" + bean.getClass().getSimpleName() + "】" +
beanName + "调用了BeanPostProcessor的postProcessBeforeInitialization()方法");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("【" + bean.getClass().getSimpleName() + "】" +
beanName + "调用了BeanPostProcessor的postProcessAfterInitialization方法");
return bean;
}
}
- MakeJuice类, 该类负责制作果汁
public class JuiceMake implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
@Autowired
private Source source;
public JuiceMake(Source source) {
this.source = source;
}
public JuiceMake() {
}
public Source getSource() {
return source;
}
public void setSource(Source source) {
this.source = source;
}
public void init() {
System.out.println(this.getClass().getSimpleName() +
"执行自定义初始化方法");
}
public void myDestory() {
System.out.println(this.getClass().getSimpleName() +
"执行自定义初销毁方法");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println(this.getClass().getSimpleName() +
"执行BeanFactoryAware接口的setBeanFactory()方法");
}
public void setBeanName(String s) {
System.out.println(this.getClass().getSimpleName() +
"执行BeanNameAware接口的setName()方法");
}
public void destroy() throws Exception {
System.out.println(this.getClass().getSimpleName() +
"执行DisposableBean接口的destroy()方法");
}
public void afterPropertiesSet() throws Exception {
System.out.println(this.getClass().getSimpleName() +
"执行InitializingBean接口的afterPropertiesSet()方法");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(this.getClass().getSimpleName() +
"执行ApplicationContextAware接口的setApplicationContext()方法");
}
}
4.部分spring配置文件
<!--PostProcessor实现类-->
<bean class="com.senchen365.beanlife.BeanPostProcImpl"/>
<!--Source类-->
<bean class="com.senchen365.beanlife.Source" id="source">
<property name="name" value="橙汁"/>
<property name="size" value="中杯"/>
</bean>
<!--MakeJuice类,定义初始化和销毁方法-->
<bean class="com.senchen365.beanlife.JuiceMake"
init-method="init" destroy-method="myDestory"/>
5.输出
//BeanPostProcessor接口的实现方法
【Source】source调用了BeanPostProcessor的postProcessBeforeInitialization()方法
【Source】source调用了BeanPostProcessor的postProcessAfterInitialization方法
JuiceMake执行BeanNameAware接口的setName()方法
JuiceMake执行BeanFactoryAware接口的setBeanFactory()方法
JuiceMake执行ApplicationContextAware接口的setApplicationContext()方法
//BeanPostProcessor接口的实现方法,JuiceMake类并未实现BeanPostProcessor,说明该接口的方法对所有bean都有效
【JuiceMake】com.senchen365.beanlife.JuiceMake#0调用了BeanPostProcessor的postProcessBeforeInitialization()方法
JuiceMake执行InitializingBean接口的afterPropertiesSet()方法
JuiceMake执行自定义初始化方法
【JuiceMake】com.senchen365.beanlife.JuiceMake#0调用了BeanPostProcessor的postProcessAfterInitialization方法
JuiceMake执行DisposableBean接口的destroy()方法
JuiceMake执行自定义初销毁方法
如果觉得不错也可以了解一下我的另一篇文章,mybatis核心组件及其生命周期
参考书籍:
JavaEE互联网轻量级框架整合开发 – 杨开阵、周吉文、梁华辉、谭茂华著
Spring In Action(第4版)[美] Craig Walls 著 张卫滨 译