Spring容器中的Bean几种初始化方法和销毁方法的先后顺序

Spring 容器中的 Bean 是有生命周期的,Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:

  • 通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
  • 通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
  • 在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。

这几种配置方式,执行顺序是怎样的呢?我们通过例子来研究下:

Java类:

 

[java]  view plain  copy
  1. import javax.annotation.PostConstruct;  
  2. import javax.annotation.PreDestroy;  
  3.   
  4. import org.springframework.beans.factory.DisposableBean;  
  5. import org.springframework.beans.factory.InitializingBean;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. public class InitAndDestroySeqBean implements InitializingBean,DisposableBean {  
  9.   
  10.     public InitAndDestroySeqBean(){  
  11.         System.out.println("执行InitAndDestroySeqBean: 构造方法");  
  12.     }  
  13.       
  14.     @PostConstruct  
  15.     public void postConstruct() {    
  16.        System.out.println("执行InitAndDestroySeqBean: postConstruct");    
  17.     }    
  18.       
  19.     @Override  
  20.     public void afterPropertiesSet() throws Exception {  
  21.         System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");   
  22.     }  
  23.       
  24.     public void initMethod() {  
  25.         System.out.println("执行InitAndDestroySeqBean: init-method");  
  26.     }  
  27.   
  28.     @PreDestroy  
  29.     public void preDestroy()  {  
  30.         System.out.println("执行InitAndDestroySeqBean: preDestroy");  
  31.     }  
  32.       
  33.     @Override  
  34.     public void destroy() throws Exception {  
  35.         System.out.println("执行InitAndDestroySeqBean: destroy");  
  36.     }  
  37.       
  38.     public void destroyMethod() {  
  39.         System.out.println("执行InitAndDestroySeqBean: destroy-method");  
  40.     }  
  41.       
  42.     public static void main(String[] args) {  
  43.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");  
  44.         context.close();  
  45.     }  
  46. }  

Spring配置文件:

 

 

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.      xmlns:context="http://www.springframework.org/schema/context"    
  5.      xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  7.  http://www.springframework.org/schema/context    
  8.  http://www.springframework.org/schema/context/spring-context-2.5.xsd">    
  9.    
  10.    <context:annotation-config/>    
  11.      
  12.    <bean id="initAndDestroySeqBean" class="com.chj.spring.InitAndDestroySeqBean" init-method="initMethod" destroy-method="destroyMethod"/>  
  13. </beans>  

运行InitAndDestroySeqBean的main方法,结果如下:

 

 

[html]  view plain  copy
  1. 2013-03-03 17:11:19,098 DEBUG support.DefaultListableBeanFactory - Creating instance of bean 'initAndDestroySeqBean'  
  2. 执行InitAndDestroySeqBean: 构造方法  
  3. 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found init method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()  
  4. 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found destroy method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()  
  5. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Eagerly caching bean 'initAndDestroySeqBean' to allow for resolving potential circular references  
  6. 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()  
  7. 执行InitAndDestroySeqBean: postConstruct  
  8. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'initAndDestroySeqBean'  
  9. 执行InitAndDestroySeqBean: afterPropertiesSet  
  10. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking init method  'initMethod' on bean with name 'initAndDestroySeqBean'  
  11. 执行InitAndDestroySeqBean: init-method  
  12. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Finished creating instance of bean 'initAndDestroySeqBean'  
  13. 2013-03-03 17:11:19,129 INFO  support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@56a499: display name [org.springframework.context.support.ClassPathXmlApplicationContext@56a499]; startup date [Sun Mar 03 17:11:17 CST 2013]; root of context hierarchy  
  14. 2013-03-03 17:11:19,129 INFO  support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1292d26: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,initAndDestroySeqBean]; root of factory hierarchy  
  15. 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking destroy method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()  
  16. 执行InitAndDestroySeqBean: preDestroy  
  17. 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy() on bean with name 'initAndDestroySeqBean'  
  18. 执行InitAndDestroySeqBean: destroy  
  19. 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy method 'destroyMethod' on bean with name 'initAndDestroySeqBean'  
  20. 执行InitAndDestroySeqBean: destroy-method  

从执行结果可以看出:

 

Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method

Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method

猜你喜欢

转载自youyanweixiao.iteye.com/blog/2291788