`
fanjf
  • 浏览: 297163 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

源码解析:init-method、@PostConstruct、afterPropertiesSet孰先孰后

 
阅读更多

Spring 容器中的 Bean 是有生命周期的 Spring  允许在 Bean  在初始化完成后以及 Bean  销毁前执行特定的操作,常用的设定

方式有以下三种:

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

这是我们就有个疑问,这三种方式是完全等同的吗,孰先孰后?

 

下面我们将带着这个疑问,试图通过测试代码以及分析 Spring 源码找到答案。

 

首先,我们还是编写一个简单的测试代码:

Java代码  
  1. public   class  InitSequenceBean  implements  InitializingBean {   
  2.     
  3.      public  InitSequenceBean() {   
  4.        System.out.println( "InitSequenceBean: constructor" );   
  5.     }   
  6.       
  7.      @PostConstruct   
  8.      public   void  postConstruct() {   
  9.        System.out.println( "InitSequenceBean: postConstruct" );   
  10.     }   
  11.       
  12.      public   void  initMethod() {   
  13.        System.out.println( "InitSequenceBean: init-method" );   
  14.     }   
  15.       
  16.      @Override   
  17.      public   void  afterPropertiesSet()  throws  Exception {   
  18.        System.out.println( "InitSequenceBean: afterPropertiesSet" );   
  19.     }   
  20. }  

public class InitSequenceBean implements InitializingBean {

public InitSequenceBean() {

System.out.println("InitSequenceBean: constructor");

}

@PostConstruct

public void postConstruct() {

System.out.println("InitSequenceBean: postConstruct");

}

public void initMethod() {

System.out.println("InitSequenceBean: init-method");

}

@Override

public void afterPropertiesSet() throws Exception { System.out.println("InitSequenceBean: afterPropertiesSet"); }

}

  

并且在配置文件中添加如下 Bean 定义:

< bean class = "InitSequenceBean" init-method = "initMethod" ></ bean >

 

好了,我们启动 Spring 容器,观察输出结果,就可知道三者的先后顺序了:

InitSequenceBean: constructor

InitSequenceBean: postConstruct

InitSequenceBean: afterPropertiesSet

InitSequenceBean: init-method

 

通过上述输出结果,三者的先后顺序也就一目了然了:

Constructor > @PostConstruct > InitializingBean > init-method

 

先大致分析下为什么会出现这些的结果:构造器( Constructor )被率先调用毋庸置疑, InitializingBean 先于 init-method

们也可以理解(在 也谈 Spring 容器的生命周期 中已经讨论过),但是 PostConstruct 为何率先于 InitializingBean 执行呢?

 

我们再次带着这个疑问去查看 Spring 源代码来一探究竟。

 

通过 Debug 并查看调用栈,我们发现了这个类 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor ,从命名上,我们就可以得到某些信

息——这是一个 BeanPostProcessor 。想到了什么?在 也谈 Spring 容器的生命周期 中,我们提到过 BeanPostProcessor postProcessBeforeInitialization 是在 Bean 生命周期中 afterPropertiesSet init-method 之前执被调用的。

 

再次观察 CommonAnnotationBeanPostProcessor 这个类,它继承自 InitDestroyAnnotationBeanPostProcessor InitDestroyAnnotationBeanPostProcessor 顾名思义,就是在 Bean 初始化和销毁的时候所作的一个前置 / 后置处理器。

 

通过查看 InitDestroyAnnotationBeanPostProcessor 类下的 postProcessBeforeInitialization 方法:

Java代码  
  1. public  Object postProcessBeforeInitialization(Object bean, String beanName)  throws  BeansException {   
  2.        LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());   
  3.         try  {   
  4.            metadata.invokeInitMethods(bean, beanName);   
  5.        }   
  6.         catch  (InvocationTargetException ex) {   
  7.             throw   new  BeanCreationException(beanName,  "Invocation of init method failed" , ex.getTargetException());   
  8.        }   
  9.         catch  (Throwable ex) {   
  10.             throw   new  BeanCreationException(beanName,  "Couldn't invoke init method" , ex);   
  11.        }   
  12.          return  bean;   
  13.     }  
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {       LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());       try {           metadata.invokeInitMethods(bean, beanName);       }       catch (InvocationTargetException ex) {           throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());       }       catch (Throwable ex) {           throw new BeanCreationException(beanName, "Couldn't invoke init method", ex);       }        return bean;    }

  

查看 findLifecycleMetadata 方法,继而我们跟踪到 buildLifecycleMetadata 这个方法体中,看下 buildLifecycleMetadata 这个方法体的内容:

Java代码  
  1. private  LifecycleMetadata buildLifecycleMetadata( final  Class clazz) {   
  2.         final  LifecycleMetadata newMetadata =  new  LifecycleMetadata();   
  3.         final   boolean  debug = logger.isDebugEnabled();   
  4.        ReflectionUtils.doWithMethods(clazz,  new  ReflectionUtils.MethodCallback() {   
  5.             public   void  doWith(Method method) {   
  6.                if  (initAnnotationType !=  null ) {   
  7.                    if  (method.getAnnotation(initAnnotationType) !=  null ) {   
  8.                      newMetadata.addInitMethod(method);   
  9.                       if  (debug) {   
  10.                          logger.debug( "Found init method on class ["  + clazz.getName() +  "]: "  + method);   
  11.                      }   
  12.                   }   
  13.               }   
  14.                if  (destroyAnnotationType !=  null ) {   
  15.                    if  (method.getAnnotation(destroyAnnotationType) !=  null ) {   
  16.                      newMetadata.addDestroyMethod(method);   
  17.                       if  (debug) {   
  18.                          logger.debug( "Found destroy method on class ["  + clazz.getName() +  "]: "  + method);   
  19.                      }   
  20.                   }   
  21.               }   
  22.            }   
  23.        });   
  24.         return  newMetadata;   
  25. }  

private LifecycleMetadata buildLifecycleMetadata(final Class clazz) {

final LifecycleMetadata newMetadata = new LifecycleMetadata();

final boolean debug = logger.isDebugEnabled();

ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {

public void doWith(Method method) {

if (initAnnotationType != null) {

if (method.getAnnotation(initAnnotationType) != null) {

newMetadata.addInitMethod(method);

if (debug) {

logger.debug("Found init method on class [" + clazz.getName() + "]: " + method);

 }

}

}

if (destroyAnnotationType != null) {

if (method.getAnnotation(destroyAnnotationType) != null) { newMetadata.addDestroyMethod(method); if (debug) { logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method); } } } } }); return newMetadata;}

  

分析这段代码发现,在这里会去判断某方法有没有被 initAnnotationType/destroyAnnotationType 注释,如果有,则添加

init/destroy 队列中,后续一一执行。

 

initAnnotationType/destroyAnnotationType 注释是什么呢,我们在 CommonAnnotationBeanPostProcessor 的构造函数

中看到下面这段代码:

Java代码  
  1. public  CommonAnnotationBeanPostProcessor() {   
  2.        setOrder(Ordered.LOWEST_PRECEDENCE -  3 );   
  3.        setInitAnnotationType(PostConstruct. class );   
  4.        setDestroyAnnotationType(PreDestroy. class );   
  5.        ignoreResourceType( "javax.xml.ws.WebServiceContext" );   
  6. }  

public CommonAnnotationBeanPostProcessor() {

setOrder(Ordered.LOWEST_PRECEDENCE - 3);

setInitAnnotationType(PostConstruct.class);

setDestroyAnnotationType(PreDestroy.class);

ignoreResourceType("javax.xml.ws.WebServiceContext");

}

  

一切都清晰了吧。一言以蔽之, @PostConstruct 注解后的方法在 BeanPostProcessor 前置处理器中就被执行了,所以当然

要先于 InitializingBean init-method 执行了。

 

最后,给出本文的结论, Bean 在实例化的过程中:

Constructor > @PostConstruct > InitializingBean > init-method

 

本文源代码下载: https://lb-multi-demo.googlecode.com/svn/trunk/spring-lifecycle-test

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics