#Spring生命周期回调说明
如果只是简单的对象初始化,我们可以将其放到构造器中处理;如果是对注入的类或者帮助类做一些初始化处理,可以考虑使用初始化方法。
Spring提供了很多的扩展点,其中就有生命周期回调,我们可以在bean初始化之前做一些处理,在bean销毁之前做一些处理。
#早期Spring生命周期扩展方式
InitializingBean: 允许在bean实例化并且设置好所有的必要属性之后,执行初始化一些处理。 DisposableBean: bean销毁前获得一次回调,做一些处理。
#initMethod和destroyMethod
当初我们使用<bean/>标签来配置bean的时候,我们可以通过init-method和destroy-method来指定bean自定义的回调方法。 当使用@Bean注解来配置bean的时候,我们通过
@Bean(initMethod="customInit")和@Bean(destroyMethod="customDestroy")来指定bean的自定义回调方法。
#推荐扩展方式
@PostStruct: InitializingBean的替代方案 @PreDestroy:DisposableBean的替代方案
#几种回调方式的顺序 可以看到Spring提供的回调方式有很多种,但是在处理这些回调的时候顺序是怎样的呢,在不看源码之前,我们可以通过实验来看一下执行顺序。
1public class BeanOrder implements InitializingBean, DisposableBean{ 2 3 public void customInit(){ 4 System.out.println("***我是通过配置init-method来实现的初始化方法。"); 5 } 6 7 public void customDestroy(){ 8 System.out.println("***我是通过配置destroy-method来实现的初始化方法。"); 9 } 10 11 @PostConstruct 12 public void initMethod(){ 13 System.out.println("***我是加了@PostConstruct注解的init方法。"); 14 } 15 16 @PreDestroy 17 public void destroyMethod(){ 18 System.out.println("***我是加了@PreDestroy注解的destroy方法。"); 19 } 20 21 @Override 22 public void destroy() throws Exception { 23 System.out.println("***我是实现DisposableBean接口的destroy方法。"); 24 25 } 26 27 @Override 28 public void afterPropertiesSet() throws Exception { 29 System.out.println("***我是实现InitializingBean接口的afterPropertiesSet方法。"); 30 } 31} 32
#####执行结果如下:
1***我是加了@PostConstruct注解的init方法。 2***我是实现InitializingBean接口的afterPropertiesSet方法。 3***我是通过配置init-method来实现的初始化方法。 4 5***我是加了@PreDestroy注解的destroy方法。 6***我是实现DisposableBean接口的destroy方法。 7***我是通过配置destroy-method来实现的初始化方法。
#代码演练 ####InitializingBean
1/** 2 * 3 * Spring官方已经不推荐这种方式来扩展Bean的初始化操作。 4 * @author Xiaoyang.Li 5 * 6 * https://github.com/superliyang 7 */ 8@Component 9public class UseInitializingBean implements InitializingBean{ 10 public void afterPropertiesSet() throws Exception { 11 System.out.println("实现InitializingBean接口来完成初始化操作,跟@PostConstruct意义一样。"); 12 } 13}
####DisposableBean
1@Component 2public class UseDisposableBean implements DisposableBean { 3 @Override 4 public void destroy() throws Exception { 5 System.out.println("实现DisposableBean接口来实现bean销毁之前做一些操作。"); 6 } 7}
当然了,一般很少单独使用DisposableBean,很多时候我们只是单独使用InitializingBean,或者同时使用InitializingBean和DisposableBean 。
####@PostConstruct and PreDestroy
1/** 2 * 生命周期回调(Bean lifecycle callback) 3 * Bean的生命周期扩展是很重要的,首先要知道,bean在生命周期有哪些地方可以自定义扩展。 4 * Bean通过@PostConstruct和@PreDestroy来扩展生命周期内的操作。 5 * 关于使用构造器和初始化方法的分析:如果只是简单的对象初始化,我们可以将其放到构造器中处理;如果是对注入的类或者帮助类做一些初始化处理,可以考虑使用初始化方法。 6 * @author Xiaoyang.Li (https://github.com/superliyang) 7 * 8 */ 9@Component 10public class BeanLifeCycleExample { 11 12 public BeanLifeCycleExample(){ 13 System.out.println("初始化构造器"); 14 } 15 16 @PostConstruct 17 public void initMethod(){ 18 //该方式跟在xml中bean标签上设置init-method是一样的。 19 //<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/> 20 System.out.println("@PostConstruct允许容器bean创建并设置好所有必要属性之后,做出一些初始化处理"); 21 } 22 23 @PreDestroy 24 public void destroyMethod(){ 25 //该方式跟在xml中bean标签上设置destroy-method是一样的。 26 //<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/> 27 System.out.println("@PreDestroy允许在bean销毁之前做出一些处理"); 28 } 29 30 public void postProcessBeforeInitializationTest(){ 31 System.out.println("在bean实例化完成,调用初始化方法之前,来调用我吧。"); 32 } 33 34}