Spring bean處理——回調函數

Spring bean處理——回調函數

 

     Spring中定義了三個可以用來對Spring bean或生成beanBeanFactory進行處理的接口, InitializingBeanBeanPostProcessorBeanFactoryPostProcessor。通過實現這三個接口我們就可以對Spring bean進行處理了。

 

InitializingBean接口

       InitializingBean中定義了一個afterPropertiesSet()方法。當BeanFactory將我們的bean實例化並且設置好了對應的屬性之後,如果我們的bean實現了InitializingBean接口,則會調用對應的afterPropertiesSet()方法。那麼我們就可以在這個方法體裏面對當前bean的屬性進行更改等其它操作。

@Component("beanA")
public class BeanA implements InitializingBean {
 
   /**
    * 回調函數,會在bean屬性設置完成後調用
    */
   public void afterPropertiesSet() throws Exception {
      System.out.println("回調函數,會在bean屬性設置完成後調用");
   }
 
}

  

BeanPostProcessor接口

       BeanPostProcessor接口實現類可以在bean初始化前後對bean做一些處理。ApplicationContext可以自動檢測到其中的bean是否已實現了BeanPostProcessor接口,如果已經實現了該接口它會自動把它當做一個BeanPostProcessor進行處理。然後在需要調用BeanPostProcessor時進行調用。BeanPostPorcessor中定義了兩個方法,postProcessBeforeInitialization()postProcessAfterInitialization()

l  postProcessBeforeInitialization(Object bean, String beanName)方法將在調用bean的初始化方法之前被調用。方法參數分別表示當前的bean對象和對應的bean名稱。

l  postProcessAfterInitialization(Object bean, String beanName)方法將在調用bean的初始化方法之後被調用。

       BeanPostProcessor是針對於容器中的所有的bean的。一旦容器中定義有BeanPostProcessor,那麼容器中的每一個bean在初始化前後都會調用BeanPostProcessor對應的方法。

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
 
   /**
    * 任何bean對象在初始化方法回調之後都會調用BeanPostProcessor的
    * postProcessAfterInitialization方法。我們可以在該方法體裏面對返回的bean再做一層封裝。
    * 調用該方法前,我們傳遞進來的bean對象是已經填充好了屬性值的。當我們把BeanPostProcessor作爲
    * 一個bean定義在ApplicationContext中時,ApplicationContext會自動檢測到它並把它當做
    * 一個BeanPostProcessor進行調用。
    */
   public Object postProcessAfterInitialization(Object bean, String beanName)
         throws BeansException {
      System.out.println(bean + "after initialization, beanName is " + beanName);
      return bean;
   }
 
   /**
    * 任何bean對象在初始化方法回調之前都會調用BeanPostProcessor的
    * postProcessBeforeInitialization方法。調用該方法前,我們傳遞進來的
    * bean對象是已經填充好了屬性值的。
    */
   public Object postProcessBeforeInitialization(Object bean, String beanName)
         throws BeansException {
      System.out.println(bean + "beforeInitialization, beanName is " + beanName);
      return bean;
   }
 
}
 

 

BeanFactoryPostProcessor接口

       BeanFactoryPostProcessor接口實現類可以在當前BeanFactory初始化後,bean實例化之前對BeanFactory做一些處理。BeanFactoryPostProcessor是針對於bean容器的,在調用它時,BeanFactory只加載了bean的定義,還沒有對它們進行實例化,所以我們可以通過對BeanFactory的處理來達到影響之後實例化bean的效果。跟BeanPostProcessor一樣,ApplicationContext也能自動檢測和調用容器中的BeanFactoryPostProcessor。
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
 
   /**
    * BeanFactoryPostProcessor的postProcessBeanFactory()方法會在當前BeanFactory初始化
    * 以後,並且所有的bean定義都已經被加載,但是還沒有對應的實例時被調用。所以我們可以在該方法體裏面通過
    * BeanFactory做一些操作。當我們把BeanFactoryPostProcessor作爲一個bean定義在ApplicationContext中時,
    * ApplicationContext會自動檢測到它並把它當做一個BeanFactoryPostProcessor進行調用。
    */
   public void postProcessBeanFactory(
         ConfigurableListableBeanFactory beanFactory) throws BeansException {
      System.out.println("postProcessBeanFactory......");
   }
 
}

 

 

 

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