spring BeanProcessor接口詳解

1. 簡單認識BeanProcessor

BeanProcessor的理解

BeanProcessor是spring中的一個重要接口,他有兩個接口方法一個是postProcessBeforeInitialization前置初始化,另一個是postProcessAfterInitialization後置初始化。從名稱上就可以大概清楚這個接口的作用:在一個業務流程的前後加入兩個接口方法,當執行這個業務流程時,就會觸發這兩個接口方法的執行。簡單的總結一下有兩個要點:

  1. 在業務流程中,根據BeanProcessor接口方法加在不同的位置(一般是前後),可以實現對業務邏輯的擴展。
  2. 在業務邏輯執行前,BeanProcessor的實現類必須已經被創建完成(BeanProcessor接口類必須要優先實例化)。

而在spring中,就有很多實現瞭BeanProcessor的bean,通過在重要的業務流程(如bean的生命周期流程)的前後加上BeanProcessor接口方法,就可以對業務邏輯進行修改或補充。

一個BeanProcessor的使用實例

在spring的bean生命周期中,BeanProcessor接口方法會在bean創建後的初始化方法(init-method或@PostConstruct指向的方法)前後執行before和after方法;那有沒有在bean創建前後執行的接口方法呢?答案是肯定有的,這個功能是由BeanProcessor的子接口InstantiationAwareBeanPostProcessor來實現的,他也是有before和after方法,會在bean實例化前後執行。

我們先定義一個BeanProcessor接口實現類和一個InstantiationAwareBeanPostProcessor接口實現類。

BeanPostProcessor實現類:

//net.postProcessor.CustomerPostProcessor
@Component
public class CustomerPostProcessor implements BeanPostProcessor {

  @PostConstruct
  public void init(){
   System.out.println("執行CustomerPostProcessor的PostConstruct");
  }

  public CustomerPostProcessor(){
   System.out.println("執行CustomerPostProcessor的構造方法");
  }

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======BeforeInitialization======"+ beanName);
   return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======AfterInitialization======"+ beanName);
   return bean;
  }

}

InstantiationAwareBeanPostProcessor實現類:

//net.postProcessor.CustomerInitialPostProcessor
@Component
public class CustomerInitialPostProcessor implements InstantiationAwareBeanPostProcessor {

  @PostConstruct
  public void init(){
   System.out.println("執行CustomerInitialPostProcessor的PostConstruct");
  }

  public CustomerInitialPostProcessor(){
   System.out.println("執行CustomerInitialPostProcessor的構造方法");
  }

  @Override
  public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
   System.out.println("bean初始化前執行:class為"+beanClass.getName()+"|beanName為"+beanName);
   return null;
  }

  @Override
  public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
   System.out.println("bean初始化後執行:Object為"+bean+"|beanName為"+beanName);
   return false;
  }
}

再創建一個普通的bean對象:

//net.postProcessor.FirstBean
@Component
public class FirstBean implements InitializingBean {

  private String msg = "hello";

  @PostConstruct
  public void init(){
   System.out.println("執行FirstBean的PostConstruct");
  }

  public FirstBean(){
   System.out.println("FirstBean構造方法!"+msg);
  }

  public String getMsg() {
   return msg;
  }

  public void setMsg(String msg) {
   this.msg = msg;
  }

  @Override
  public void afterPropertiesSet() throws Exception {
   System.out.println("執行FirstBean的afterPropertiesSet");
  }
}

我們創建一個spring工廠對象將上述bean加載進去:

@Test
public void test(){
  AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("net.postProcessor");
}
//執行得到以下結果:
執行CustomerInitialPostProcessor的構造方法
執行CustomerInitialPostProcessor的PostConstruct
執行CustomerPostProcessor的構造方法
執行CustomerPostProcessor的PostConstruct
  
bean初始化前執行:class為net.postProcessor.FirstBean|beanName為firstBean
FirstBean構造方法!hello
bean初始化後執行:Object為net.postProcessor.FirstBean@79179359|beanName為firstBean
  
net.postProcessor.FirstBean@79179359======BeforeInitialization======firstBean
執行FirstBean的PostConstruct
執行FirstBean的afterPropertiesSet
net.postProcessor.FirstBean@79179359======AfterInitialization======firstBean

通過上述結果證明瞭我們之前的說法是正確的:

1.BeanPostProcessor接口類會優先實例化,且在實例化中無法不會調用BeanPostProcessor接口方法的

2.InstantiationAwareBeanPostProcessor接口方法會在FirstBean構造方法構造方法前後執行

3.BeanPostProcessor接口方法會在FirstBean實例化後進行初始化的前後執行

註意:若@PostConstruct註解方法方法未執行,請加入javax.annotation:javax.annotation-api:1.3.2jar包依賴,原因是@PostConstruct是J2EE標準的註解,不是spring自己的接口,而在JDK8往上的版本中設計者打算棄用這些註解,所以做瞭處理,我們是沒有辦法直接使用J2EE標準註解的(@Resource、@PostConstruct、@PreDestroy等幾個註解),為瞭兼容這種情況,所以有瞭javax.annotation-apijar包的產生(或者降低JDK版本)。

2. BeanProcessor的實現思路和簡化實例

BeanProcessor大概的實現思路

通過之前的瞭解BeanProcessor的使用,我們可以知道BeanProcessor並不復雜,但是卻十分的重要,下面來分析下BeanProcessor的實現思路:

  1. 創建個接口A,接口包含一些切點方法(Before、After、Around之類的),實現這個接口A的類要在使用前就創建好
  2. 我們需要有個業務流程,這個業務流程由若幹步組成;將接口A的接口方法插入到這些業務步驟之間(需要擴展的地方)
  3. 要執行這個業務流程時,把接口A的實現類對象賦值到業務流程中,在執行業務流程中,就會觸發接口方法的執行完成功能擴展

當我們更換賦值到業務流程中的接口A的實現類時,對應的擴展邏輯也會隨之變化,這樣就實現瞭可插拔式的擴展邏輯(策略模式)。

一個BeanProcessor的簡化邏輯實例

在spring中我們可以創建任意數量的bean實現BeanProcessor接口,所以實際上我們是要一個全局的beanProcessorList對象用來存儲這些BeanProcessor對象;在執行業務代碼時,要循環這個beanProcessorList對象,獲取你需要的BeanProcessor對象來執行接口方法。下面是一個模擬spring bean生命周期的簡化版,來幫助你理解spring中BeanProcessor的工作原理。

net.postProcessor.SecondBean.java

@Component
public class SecondBean {

  private String msg = "world";

  public SecondBean(){
   System.out.println("SecondBean構造方法!"+msg);
  }

  public String getMsg() {
   return msg;
  }

  public void setMsg(String msg) {
   this.msg = msg;
  }
}

net.postProcessor.CustomerPostProcessor.java

@Component
public class CustomerPostProcessor implements BeanPostProcessor {

  @PostConstruct
  public void init(){
   System.out.println("執行CustomerPostProcessor的PostConstruct");
  }

  public CustomerPostProcessor(){
   System.out.println("執行CustomerPostProcessor的構造方法");
  }

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======BeforeInitialization======"+ beanName);
   return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======AfterInitialization======"+ beanName);
   return bean;
  }

}

net.postProcessor.PostProcessor.java

public class PostProcessor {

  //模擬掃描到的bean信息<"SecondBean", "net.postProcessor.SecondBean">
  Map<String, String> scanBeanMap = new HashMap<>();

  //模擬spring的beanPostProcessors列表
  List<BeanPostProcessor> processorBeanList = new ArrayList<>();

  //模擬bean對象緩存
  Map<String, Object> beanCache = new HashMap<>();

  //添加掃描的bean信息
  public PostProcessor addBeanInfo(String beanName, String classPath){
   this.scanBeanMap.put(beanName, classPath);
   return this;
  }

  //模擬bean創建流程
  public Object execute(){
   try {
     //先臨時存儲實現瞭postProcessor接口的bean對象
     List<BeanPostProcessor> postProcessorStrList = new ArrayList<>();
     //循環scanBeanMap,獲取bean列表中實現瞭postProcessor接口的類,加入processorBeanList中
     for(String temp: scanBeanMap.keySet()){
      Class<?> clazz = Class.forName(scanBeanMap.get(temp));
      //判斷是否實現瞭BeanPostProcessor接口
      if(BeanPostProcessor.class.isAssignableFrom(clazz)){
        //實例化讓如臨時容器
        postProcessorStrList.add((BeanPostProcessor)createBean(temp));
      }
     }
     //將實現瞭postProcessor接口的bean加入processorBeanList中
     for(BeanPostProcessor obj: postProcessorStrList){
      processorBeanList.add(obj);
     }

     //再次循環scanBeanMap初始化所用bean
     for(String temp: scanBeanMap.keySet()){
      createBean(temp);
     }

   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   }
   return null;
  }

  //bean實例化
  public Object createBean(String beanName){
   //從緩存中獲取
   if(beanCache.containsKey(beanName)){
     return beanCache.get(beanName);
   }else{
     //緩存中取不到,則進行創建後加入緩存
     try {
      Class<?> clazz = Class.forName(scanBeanMap.get(beanName));
      //processor前置方法執行
      for(BeanPostProcessor processor : processorBeanList){
        processor.postProcessBeforeInitialization(clazz, beanName);
      }

      //bean實例化
      Object result = clazz.getConstructor().newInstance();

      //processor後置方法執行
      for(BeanPostProcessor processor : processorBeanList){
        processor.postProcessAfterInitialization(result, beanName);
      }

      //將bean加入緩存
      beanCache.put(beanName, result);
      return result;
     } catch (ClassNotFoundException e) {
      e.printStackTrace();
     } catch (IllegalAccessException e) {
      e.printStackTrace();
     } catch (InstantiationException e) {
      e.printStackTrace();
     } catch (NoSuchMethodException e) {
      e.printStackTrace();
     } catch (InvocationTargetException e){
      e.printStackTrace();
     }
   }
   return null;
  }

}

代碼調用

public static void main(String[] args) {
  PostProcessor postProcessor = new PostProcessor();
  //添加掃描到的bean
  postProcessor
  .addBeanInfo("SecondBean", "net.postProcessor.SecondBean")
  .addBeanInfo("CustomerPostProcessor", "net.postProcessor.CustomerPostProcessor");
  postProcessor.execute();
}

//執行結果
執行CustomerPostProcessor的構造方法
class net.postProcessor.SecondBean======BeforeInitialization======SecondBean
SecondBean構造方法!world
net.postProcessor.SecondBean@1b40d5f0======AfterInitialization======SecondBean

代碼邏輯如下:

  1. 循環bean信息列表,將BeanPostProcessor接口bean分離出來優先實例化(實例化中緩存bean對象),並將之放入臨時容器。
  2. 循環完成,將臨時容器中的BeanPostProcessor接口bean賦值到全局BeanPostProcessor接口列表中
  3. 再次循環bean信息列表,緩存存在則直接返回緩存對象,不存在則進行bean實例化,期間循環調用全局BeanPostProcessor接口對象方法

3. spring中BeanProcessor的源碼解析

我們要從spring中的refresh()開始看起:

public void refresh() throws BeansException, IllegalStateException {
  synchronized (this.startupShutdownMonitor) {
   // Prepare this context for refreshing.
   //刷新準備
   prepareRefresh();

   // Tell the subclass to refresh the internal bean factory.
   //告訴子類刷新內部bean工廠。
   ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

   // Prepare the bean factory for use in this context.
   //為容器準備bean工程
   prepareBeanFactory(beanFactory);

   try {
     // Allows post-processing of the bean factory in context subclasses.
     //允許在上下文bean的後處理工廠子類。
     postProcessBeanFactory(beanFactory);

     // Invoke factory processors registered as beans in the context.
     //優先將BeanDefinitionRegistryPostProcessor\BeanFactoryPostProcessor接口的bean對象實例化
     //屬於spring內部組件調用
     invokeBeanFactoryPostProcessors(beanFactory);

     // Register bean processors that intercept bean creation.
     //處理用戶自定義PostProcessor接口對象,之後加入spring的beanPostProcessors列表,
     // 供之後預實例化其他bean時觸發這些PostProcessor方法
     registerBeanPostProcessors(beanFactory);

		//...省略代碼
    //實例化所有(non-lazy-init)單件。
		finishBeanFactoryInitialization(beanFactory);
   }

   catch (BeansException ex) {
     if (logger.isWarnEnabled()) {
      logger.warn("Exception encountered during context initialization - " +
         "cancelling refresh attempt: " + ex);
     }

     // Destroy already created singletons to avoid dangling resources.
     //bean銷毀
     destroyBeans();

     // Reset 'active' flag.
     //取消刷新
     cancelRefresh(ex);

     // Propagate exception to caller.
     throw ex;
   }

   finally {
     // Reset common introspection caches in Spring's core, since we
     // might not ever need metadata for singleton beans anymore...
     //重置公共緩存
     resetCommonCaches();
   }
  }
}

其中包含有postProcess字段都有可能和BeanProcessor相關,這裡有三個相關方法:

  1. postProcessBeanFactory(beanFactory),這個是一共空的擴展方法,顯然無關
  2. invokeBeanFactoryPostProcessors(beanFactory),處理spring中實現瞭BeanProcessor接口的內部組件直接調用接口方法
  3. registerBeanPostProcessors(beanFactory),實例化用戶自定義BeanProcessor接口bean組件,之後循環賦值到全局BeanProcessor列表中

所以registerBeanPostProcessors()就是我們要找的對象,來跟進看下registerBeanPostProcessors():

//AbstractApplicationContext#registerBeanPostProcessors
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
  //委托給PostProcessorRegistrationDelegate.registerBeanPostProcessors進行處理
	PostProcessorRegistrationDelegate.registerBeanPostProcessors進行處理(beanFactory, this);
}
public static void registerBeanPostProcessors(
   ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

  //查詢實現瞭BeanPostProcessor接口的beanName
  String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

  // Register BeanPostProcessorChecker that logs an info message when
  // a bean is created during BeanPostProcessor instantiation, i.e. when
  // a bean is not eligible for getting processed by all BeanPostProcessors.
  int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
  beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

  // Separate between BeanPostProcessors that implement PriorityOrdered,
  // Ordered, and the rest.
  List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
  List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
  List<String> orderedPostProcessorNames = new ArrayList<>();
  List<String> nonOrderedPostProcessorNames = new ArrayList<>();
  //根據beanName循環調用getBean進行實例化
  for (String ppName : postProcessorNames) {
   if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
     BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
     priorityOrderedPostProcessors.add(pp);
     if (pp instanceof MergedBeanDefinitionPostProcessor) {
      internalPostProcessors.add(pp);
     }
   }
   else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
     orderedPostProcessorNames.add(ppName);
   }
   else {
     nonOrderedPostProcessorNames.add(ppName);
   }
  }

  // First, register the BeanPostProcessors that implement PriorityOrdered.
  //對BeanPostProcessor接口對象進行排序
  sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
  //將獲取到的PostProcessors接口對象加入到spring的beanPostProcessors列表
  registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

  // Next, register the BeanPostProcessors that implement Ordered.
  List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
  for (String ppName : orderedPostProcessorNames) {
   BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
   orderedPostProcessors.add(pp);
   if (pp instanceof MergedBeanDefinitionPostProcessor) {
     internalPostProcessors.add(pp);
   }
  }
  sortPostProcessors(orderedPostProcessors, beanFactory);
  registerBeanPostProcessors(beanFactory, orderedPostProcessors);

  // Now, register all regular BeanPostProcessors.
  List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
  for (String ppName : nonOrderedPostProcessorNames) {
   BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
   nonOrderedPostProcessors.add(pp);
   if (pp instanceof MergedBeanDefinitionPostProcessor) {
     internalPostProcessors.add(pp);
   }
  }
  registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

  // Finally, re-register all internal BeanPostProcessors.
  sortPostProcessors(internalPostProcessors, beanFactory);
  registerBeanPostProcessors(beanFactory, internalPostProcessors);

  // Re-register post-processor for detecting inner beans as ApplicationListeners,
  // moving it to the end of the processor chain (for picking up proxies etc).
  beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

果然這裡就是處理BeanPostProcessor接口的地方,邏輯和之前的思路類似:

  1. 循環掃描到的bean列表,獲取實現瞭BeanPostProcessor接口的beanName數組
  2. 循環beanName數組數組,調用beanFactory.getBean()將bean實例化,並放入priorityOrderedPostProcessors列表中
  3. 調用sortPostProcessors對priorityOrderedPostProcessors列表進行排序(處理BeanPostProcessor調用的順序)
  4. 調用registerBeanPostProcessors將priorityOrderedPostProcessors列表中的bean對象賦值到全局列表beanPostProcessors中
  5. 回到refresh()中,當調用finishBeanFactoryInitialization()對所用bean進行預實例化時就會調用這些BeanPostProcessor接口方法

以上就是spring BeanProcessor接口詳解的詳細內容,更多關於spring BeanProcessor接口的資料請關註WalkonNet其它相關文章!

推薦閱讀:

    None Found