Spring Bean的一生
When you work directly in Java, you can do anything you like with your objects and do not always need to rely on the container lifecycle.
前言:
在Ioc容器启动后相应的Bean并没有立即实例化,此时Ioc容器仅仅拥有所有对象的BeanDefinition(Bean对象在Spring中的描述,包含该Bean在容器中实例化所需的信息,通过解析xml/javaconfig后所得)
Spring Bean生命周期的管理
Spring bean factory
能管理Beans
的生命周期,通过实现InitializingBean和DisposableBean
这两个接口都只声明一个方法让我们可以初始化/关闭
bean中的资源
当然在整个生命周期中也提供了一些Call Back Methods,这些方法类似于Servlet
中的监听器的实现
- 当
Bean
处于post-initialization
阶段时,我们可以通过实现InitializingBean
接口来实现afterPropertiesSet()
- 当
Bean
处于pre-destruction
阶段时,我们可以实现DisposableBean
接口来实现destroy()
方法
Spring Bean的生命周期图
Code Time
以Rick
类交给Spring容器管理,方法顺序为其在Spring容器中的执行顺序
Rick.java
public class Rick implements InitializingBean, DisposableBean {
public Rick(){
System.out.println("Rick is in [Rick] - Rick()");
}
public void customInit(){
System.out.println("Rick is in [Rick] - customInit()");
}
@PostConstruct
public void postConstruct(){
System.out.println("Rick is in [@PostConstruct] - postConstruct()");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Rick is in [InitializingBean] - afterPropertiesSet()");
}
@PreDestroy
public void preDestroy(){
System.out.println("Rick is in [@PreDestroy] - preDestroy()");
}
@Override
public void destroy() throws Exception {
System.out.println("Rick is in [DisposableBean] - destroy()");
}
public void customDestroy(){
System.out.println("Rick is in [Rick] - customDestroy()");
}
}
加入BeanPostProcess
RickBeanPostProcess.java
public class RickBeanPostProcess implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Rick) {
System.out.println("Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Rick) {
System.out.println("Rick is in [BeanPostProcessor] - postProcessAfterInitialization()");
}
return bean;
}
}
配置类
RickConfig.java
@Configuration
public class RickConfig {
/**
* 指定自定义初始化方法和自定义销毁方法
* @return Rick
*/
@Bean(initMethod = "customInit",destroyMethod = "customDestroy")
public Rick rick(){
return new Rick();
}
@Bean
public RickBeanPostProcess rickBeanPostProcess(){
return new RickBeanPostProcess();
}
}
测试类
Client.java
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RickConfig.class);
((AnnotationConfigApplicationContext) applicationContext).close();
}
}
最终结果如下
Rick is in [Rick] - Rick()
Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()
Rick is in [@PostConstruct] - postConstruct()
Rick is in [InitializingBean] - afterPropertiesSet()
Rick is in [Rick] - customInit()
Rick is in [BeanPostProcessor] - postProcessAfterInitializa
Rick is in [@PreDestroy] - preDestroy()
Rick is in [DisposableBean] - destroy()
Rick is in [Rick] - customDestroy()
参考
Spring Framework Documentation