圖解Spring中bean的生命週期

Bean的生命週期

正確理解Spring bean的生命週期非常重要,因爲你或許要利用Spring提供的擴展點來完成特定需求。如下圖展示了bean 裝載到Spring應用上下文中的一個典型的生命週期過程。
在這裏插入圖片描述
接下來我們對該圖中的內容進行詳細解釋

  1. Spring對bean進行實例化;
  2. Spring將值和bean的引用注入到bean對應的屬性中;
  3. 如果bean實現了BeanNameAware接口,Spring將bean的ID傳遞給setBean-Name()方法,然後調用這個方法;
  4. 如果bean實現了BeanFactoryAware接口,Spring將調用setBeanFactory()方法,將 BeanFactory容器實例傳入;
  5. 如果bean實現了ApplicationContextAware接口,Spring將調 用setApplicationContext()方法,將bean所在的應用上下文的引用傳入進來;
  6. 如果bean實現了BeanPostProcessor接口,Spring將調用它們的postProcessBeforeInitialization()方法;
  7. 如果bean實現了InitializingBean接口,Spring將調用它們的afterPropertiesSet()方法;
  8. 如果Bean使用init-method,他就會調用已定義的初始化方法;
  9. 如果bean實現了BeanPostProcessor接口,Spring將調用它們的postProcessAfterInitialization()方法;
  10. 此時,bean已經準備就緒,可以被應用程序使用了,它們將一直駐留在應用上下文中,直到該 應用上下文被銷燬;

當服務器正常關閉,過着遇到其他關閉Spring IOC容器的事件,他就會調用對應的方法完成Bean的銷燬,其步驟如下:
11. 如果bean實現了DisposableBean接口,Spring將調用它的destroy()接口方法。
12. 同樣, 如果bean使用destroy-method聲明瞭銷燬方法,該方法也會被調用。

Bean生命週期測試實例

如果看不懂上面也沒有關係,我們接下來通過一個實例來具體看看bean的生命週期

  1. Source類,該類提供果汁的描述
public class Source {
   private String size;
   private String name;

   public String getSize() {
       return size;
   }

   public void setSize(String size) {
       this.size = size;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public Source() {
   }

   public Source(String size, String name) {
       this.size = size;
       this.name = name;
   }
}
  1. BeanPostProcessorImpl類, BeanPostProcessor接口的實現類,你會發現所有bean都會調用該實現。
public class BeanPostProcImpl implements BeanPostProcessor {

   public Object postProcessBeforeInitialization(Object bean, String beanName) 
   	throws BeansException {
       System.out.println("【" + bean.getClass().getSimpleName() + "】" + 
      			 beanName + "調用了BeanPostProcessor的postProcessBeforeInitialization()方法");
       return bean;
   }

   public Object postProcessAfterInitialization(Object bean, String beanName) 
   	throws BeansException {
       System.out.println("【" + bean.getClass().getSimpleName() + "】" + 
       		beanName + "調用了BeanPostProcessor的postProcessAfterInitialization方法");             
       return bean;
   }
}
  1. MakeJuice類, 該類負責製作果汁
public class JuiceMake implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
   @Autowired
   private Source source;

   public JuiceMake(Source source) {
       this.source = source;
   }

   public JuiceMake() {
   }

   public Source getSource() {
       return source;
   }

   public void setSource(Source source) {
       this.source = source;
   }

   public void init() {
       System.out.println(this.getClass().getSimpleName() +
               "執行自定義初始化方法");
   }

   public void myDestory() {
       System.out.println(this.getClass().getSimpleName() +
               "執行自定義初銷燬方法");
   }

   public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
       System.out.println(this.getClass().getSimpleName() +
               "執行BeanFactoryAware接口的setBeanFactory()方法");
   }

   public void setBeanName(String s) {
       System.out.println(this.getClass().getSimpleName() +
               "執行BeanNameAware接口的setName()方法");
   }

   public void destroy() throws Exception {
       System.out.println(this.getClass().getSimpleName() +
               "執行DisposableBean接口的destroy()方法");
   }

   public void afterPropertiesSet() throws Exception {
       System.out.println(this.getClass().getSimpleName() +
               "執行InitializingBean接口的afterPropertiesSet()方法");
   }

   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       System.out.println(this.getClass().getSimpleName() +
               "執行ApplicationContextAware接口的setApplicationContext()方法");
   }
}

4.部分spring配置文件

   <!--PostProcessor實現類-->
   <bean class="com.senchen365.beanlife.BeanPostProcImpl"/>

   <!--Source類-->
   <bean class="com.senchen365.beanlife.Source" id="source">
       <property name="name" value="橙汁"/>
       <property name="size" value="中杯"/>
   </bean>

   <!--MakeJuice類,定義初始化和銷燬方法-->
   <bean class="com.senchen365.beanlife.JuiceMake"
         init-method="init" destroy-method="myDestory"/>

5.輸出

//BeanPostProcessor接口的實現方法
【Source】source調用了BeanPostProcessor的postProcessBeforeInitialization()方法
【Source】source調用了BeanPostProcessor的postProcessAfterInitialization方法

JuiceMake執行BeanNameAware接口的setName()方法
JuiceMake執行BeanFactoryAware接口的setBeanFactory()方法
JuiceMake執行ApplicationContextAware接口的setApplicationContext()方法

//BeanPostProcessor接口的實現方法,JuiceMake類並未實現BeanPostProcessor,說明該接口的方法對所有bean都有效
【JuiceMake】com.senchen365.beanlife.JuiceMake#0調用了BeanPostProcessor的postProcessBeforeInitialization()方法

JuiceMake執行InitializingBean接口的afterPropertiesSet()方法
JuiceMake執行自定義初始化方法
【JuiceMake】com.senchen365.beanlife.JuiceMake#0調用了BeanPostProcessor的postProcessAfterInitialization方法

JuiceMake執行DisposableBean接口的destroy()方法
JuiceMake執行自定義初銷燬方法

如果覺得不錯也可以瞭解一下我的另一篇文章,mybatis核心組件及其生命週期
參考書籍:
JavaEE互聯網輕量級框架整合開發 – 楊開陣、周吉文、梁華輝、譚茂華著
Spring In Action(第4版)[美] Craig Walls 著 張衛濱 譯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章