spring 容器初始化 bean 和銷毀前所做的操作


第一種:通過注解@PostConstruct 和 @PreDestroy 方法 實現初始化和銷毀bean之前進行的操作

 

 

[java]  view plain  copy
 
  1. import javax.annotation.PostConstruct;    
  2. import javax.annotation.PreDestroy;    
  3.   
  4. public class DataInitializer{     
  5.     @PostConstruct  
  6.     public void initMethod() throws Exception {  
  7.         System.out.println("initMethod 被執行");  
  8.     }  
  9.     @PreDestroy  
  10.     public void destroyMethod() throws Exception {  
  11.         System.out.println("destroyMethod 被執行");  
  12.     }  
  13. }  

 

第二種是:通過 在xml中定義init-method 和  destory-method方法

[java]  view plain  copy
 
  1. public class DataInitializer{  
  2.     public void initMethod() throws Exception {  
  3.         System.out.println("initMethod 被執行");  
  4.     }  
  5.     public void destroyMethod() throws Exception {  
  6.         System.out.println("destroyMethod 被執行");  
  7.     }  
  8. }  

 

[html]  view plain  copy
 
  1. <bean id="dataInitializer" class="com.somnus.demo.DataInitializer" init-method="initMethod" destory-method="destroyMethod"/>  

 

第三種是: 通過bean實現InitializingBean和 DisposableBean接口

[java]  view plain  copy
 
  1. import org.springframework.beans.factory.DisposableBean;  
  2.   
  3. public class DataInitializer implements InitializingBean,DisposableBean{  
  4.       
  5.     @Override  
  6.     public void afterPropertiesSet() throws Exception {  
  7.         System.out.println("afterPropertiesSet 被執行");  
  8.     }  
  9.       
  10.     @Override  
  11.     public void destroy() throws Exception {  
  12.         System.out.println("destroy 被執行");  
  13.     }  
  14.   
  15. }  

 

其中第一種和第二種是同一種形式,只不過一種xml配置,另外一種采用注解形式罷了,有很大區別的是第三種,

如果同一個bean同時采用兩種方式初始化的時候執行某個方法,首先在執行順序上就會體現出來。

先執行afterPropertiesSet(),

后執行initMethod()

 

這里我們看下源碼

這方式在spring中是怎么實現的?

通過查看spring的加載bean的源碼類(AbstractAutowireCapableBeanFactory)可看出其中奧妙

AbstractAutowireCapableBeanFactory類中的invokeInitMethods講解的非常清楚,源碼如下:

[java]  view plain  copy
 
  1. protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)  
  2.       throws Throwable {  
  3.   //判斷該bean是否實現了實現了InitializingBean接口,如果實現了InitializingBean接口,則只掉調用bean的afterPropertiesSet方法  
  4.   boolean isInitializingBean = (bean instanceof InitializingBean);  
  5.   if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {  
  6.       if (logger.isDebugEnabled()) {  
  7.           logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");  
  8.       }  
  9.         
  10.       if (System.getSecurityManager() != null) {  
  11.           try {  
  12.               AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {  
  13.                   public Object run() throws Exception {  
  14.                       //直接調用afterPropertiesSet  
  15.                       ((InitializingBean) bean).afterPropertiesSet();  
  16.                       return null;  
  17.                   }  
  18.               },getAccessControlContext());  
  19.           } catch (PrivilegedActionException pae) {  
  20.               throw pae.getException();  
  21.           }  
  22.       }                  
  23.       else {  
  24.           //直接調用afterPropertiesSet  
  25.           ((InitializingBean) bean).afterPropertiesSet();  
  26.       }  
  27.   }  
  28.   if (mbd != null) {  
  29.       String initMethodName = mbd.getInitMethodName();  
  30.       //判斷是否指定了init-method方法,如果指定了init-method方法,則再調用制定的init-method  
  31.       if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&  
  32.               !mbd.isExternallyManagedInitMethod(initMethodName)) {  
  33.               //進一步查看該方法的源碼,可以發現init-method方法中指定的方法是通過反射實現  
  34.           invokeCustomInitMethod(beanName, bean, mbd);  
  35.       }  
  36.   }  


總結:

1:spring為bean提供了兩種初始化bean的方式,實現InitializingBean接口,實現afterPropertiesSet方法,或者在配置文件中同過

init-method指定,兩種方式可以同時使用


2:實現InitializingBean接口是直接調用afterPropertiesSet方法,比通過反射調用init-method指定的方法效率相對來說要高點。但是

init-method方式消除了對spring的依賴


3:如果調用afterPropertiesSet方法時出錯,則不調用init-method指定的方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM