欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?invokeBeanFactoryPostProcessors方法刨析源碼

 更新時間:2023年01月13日 11:38:35   作者:融極  
invokeBeanFactoryPostProcessors該方法會實例化所有BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor的實例并且執(zhí)行postProcessBeanFactory與postProcessBeanDefinitionRegistry方法

概述

invokeBeanFactoryPostProcessor方法是spring核心方法之一,主要用來調(diào)用beanFactory后置處理器來修改beanDefinition。

該方法實例化并調(diào)用已經(jīng)注冊到beanFactory的beanFactoryPostProcessor實例。

invokeBeanFactoryPostProcessors

	/**
	 * 實例化并且調(diào)用所有已經(jīng)注冊了的beanFactoryPostProcessor,遵循指明的順序
	 *
	 * Instantiate and invoke all registered BeanFactoryPostProcessor beans,
	 * respecting explicit order if given.
	 * <p>Must be called before singleton instantiation.
	 */
	protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		// 獲取到當前應用程序上下文的beanFactoryPostProcessors變量的值,并且實例化調(diào)用執(zhí)行所有已經(jīng)注冊的beanFactoryPostProcessor
		// 默認情況下,通過getBeanFactoryPostProcessors()來獲取已經(jīng)注冊的BFPP,但是默認是空的 
		PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
		// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
		// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
		if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}
	}

getBeanFactoryPostProcessors() 方法

方法獲取自定義的BFPP方法,默認為空。

// TODO 如何設(shè)置自定義的BFPP?

invokeBeanFactoryPostProcessors方法

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory,

List beanFactoryPostProcessors){}

參數(shù):ConfigurableListableBeanFactory beanFactory是refresh()方法中的obtainFreshBeanFactory()方法獲取的。

是DefaultListableBeanFactory 類型,DefaultListableBeanFactory類實現(xiàn)了BeanDefinitionRegistry接口。

	public final ConfigurableListableBeanFactory getBeanFactory() {
		DefaultListableBeanFactory beanFactory = this.beanFactory;
		if (beanFactory == null) {
			throw new IllegalStateException("BeanFactory not initialized or already closed - " +
					"call 'refresh' before accessing beans via the ApplicationContext");
		}
		return beanFactory;
	}

所以會走下面分支

if (beanFactory instanceof BeanDefinitionRegistry) {
...
}

否則直接調(diào)用外部beanFactoryPostProcessors的postProcessBeanFactory方法。

invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);

BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor分類

  // 類型轉(zhuǎn)換
  BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
  // 此處希望大家做一個區(qū)分,兩個接口是不同的,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子集
  // BeanFactoryPostProcessor主要針對的操作對象是BeanFactory,而BeanDefinitionRegistryPostProcessor主要針對的操作對象是BeanDefinition
  // 存放BeanFactoryPostProcessor的集合
  List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
  // 存放BeanDefinitionRegistryPostProcessor的集合
  List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
  // 首先處理入?yún)⒅械腷eanFactoryPostProcessors,遍歷所有的beanFactoryPostProcessors,將BeanDefinitionRegistryPostProcessor
  // 和BeanFactoryPostProcessor區(qū)分開
  for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
      // 如果是BeanDefinitionRegistryPostProcessor
      if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
          BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
          // 直接執(zhí)行BeanDefinitionRegistryPostProcessor接口中的postProcessBeanDefinitionRegistry方法
          registryProcessor.postProcessBeanDefinitionRegistry(registry);
          // 添加到registryProcessors,用于后續(xù)執(zhí)行postProcessBeanFactory方法
          registryProcessors.add(registryProcessor);
      } else {
          // 否則,只是普通的BeanFactoryPostProcessor,添加到regularPostProcessors,用于后續(xù)執(zhí)行postProcessBeanFactory方法
          regularPostProcessors.add(postProcessor);
      }
  }

處理實現(xiàn)了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor類

  List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
  // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
  // 調(diào)用所有實現(xiàn)PriorityOrdered接口的BeanDefinitionRegistryPostProcessor實現(xiàn)類
  // 找到所有實現(xiàn)BeanDefinitionRegistryPostProcessor接口bean的beanName
  String[] postProcessorNames =
          beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
  // 遍歷處理所有符合規(guī)則的postProcessorNames
  for (String ppName : postProcessorNames) {
      // 檢測是否實現(xiàn)了PriorityOrdered接口
      if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
          // 獲取名字對應的bean實例,添加到currentRegistryProcessors中
          currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
          // 將要被執(zhí)行的BFPP名稱添加到processedBeans,避免后續(xù)重復執(zhí)行
          processedBeans.add(ppName);
      }
  }
  // 按照優(yōu)先級進行排序操作
  sortPostProcessors(currentRegistryProcessors, beanFactory);
  // 添加到registryProcessors中,用于最后執(zhí)行postProcessBeanFactory方法
  registryProcessors.addAll(currentRegistryProcessors);
  // 遍歷currentRegistryProcessors,執(zhí)行postProcessBeanDefinitionRegistry方法
  invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
  // 執(zhí)行完畢之后,清空currentRegistryProcessors
  currentRegistryProcessors.clear();

處理實現(xiàn)了Ordered的BeanDefinitionRegistryPostProcessor類

 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 for (String ppName : postProcessorNames) {
     // 檢測是否實現(xiàn)了Ordered接口,并且還未執(zhí)行過
     if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
         // 獲取名字對應的bean實例,添加到currentRegistryProcessors中
         currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
         // 將要被執(zhí)行的BFPP名稱添加到processedBeans,避免后續(xù)重復執(zhí)行
         processedBeans.add(ppName);
     }
 }
 // 按照優(yōu)先級進行排序操作
 sortPostProcessors(currentRegistryProcessors, beanFactory);
 // 添加到registryProcessors中,用于最后執(zhí)行postProcessBeanFactory方法
 registryProcessors.addAll(currentRegistryProcessors);
 // 遍歷currentRegistryProcessors,執(zhí)行postProcessBeanDefinitionRegistry方法
 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
 // 執(zhí)行完畢之后,清空currentRegistryProcessors
 currentRegistryProcessors.clear();

處理剩下的BeanDefinitionRegistryPostProcessor類

  // 最后,調(diào)用所有剩下的BeanDefinitionRegistryPostProcessors
  boolean reiterate = true;
  while (reiterate) {
      reiterate = false;
      // 找出所有實現(xiàn)BeanDefinitionRegistryPostProcessor接口的類
      postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      // 遍歷執(zhí)行
      for (String ppName : postProcessorNames) {
          // 跳過已經(jīng)執(zhí)行過的BeanDefinitionRegistryPostProcessor
          if (!processedBeans.contains(ppName)) {
              // 獲取名字對應的bean實例,添加到currentRegistryProcessors中
              currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
              // 將要被執(zhí)行的BFPP名稱添加到processedBeans,避免后續(xù)重復執(zhí)行
              processedBeans.add(ppName);
              reiterate = true;
          }
      }
      // 按照優(yōu)先級進行排序操作
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      // 添加到registryProcessors中,用于最后執(zhí)行postProcessBeanFactory方法
      registryProcessors.addAll(currentRegistryProcessors);
      // 遍歷currentRegistryProcessors,執(zhí)行postProcessBeanDefinitionRegistry方法
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      // 執(zhí)行完畢之后,清空currentRegistryProcessors
      currentRegistryProcessors.clear();
  }

注意這里的reiterate變量在每找到一個未執(zhí)行的BeanDefinitionRegistryPostProcessor實例都會被設(shè)置為true,表示invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);方法執(zhí)行時可能會生成新的BeanDefinitionRegistryPostProcessor實例。

調(diào)用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法

  // 調(diào)用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法
  invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
  // 最后,調(diào)用入?yún)eanFactoryPostProcessors中的普通BeanFactoryPostProcessor的postProcessBeanFactory方法
  invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);

處理BeanFactory容器中注冊的BeanFactoryPostProcessor類

處理方式與上面處理BeanDefinitionRegistryPostProcessor類似,按照先后順序分別處理實現(xiàn)了PriorityOrdered接口、Ordered接口、沒有實現(xiàn)Ordered接口的bean。這里不在詳細說明。

   // 到這里為止,入?yún)eanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已經(jīng)全部處理完畢,下面開始處理容器中
   // 所有的BeanFactoryPostProcessor
   // 可能會包含一些實現(xiàn)類,只實現(xiàn)了BeanFactoryPostProcessor,并沒有實現(xiàn)BeanDefinitionRegistryPostProcessor接口,
   // 因為有processedBeans記錄了上面處理的實現(xiàn)了BeanDefinitionRegistryPostProcessor的類,所以不會重復處理。
   String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
   regularPostProcessors = new ArrayList();
   registryProcessors = new ArrayList();
   currentRegistryProcessors = new ArrayList();
   postProcessorNames = postProcessorNames;
   int var20 = postProcessorNames.length;
   String ppName;
   for(var9 = 0; var9 < var20; ++var9) {
       ppName = postProcessorNames[var9];
       if (!processedBeans.contains(ppName)) {
           if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
               regularPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
           } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
               registryProcessors.add(ppName);
           } else {
               currentRegistryProcessors.add(ppName);
           }
       }
   }
   sortPostProcessors(regularPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors((Collection)regularPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
   List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList();
   Iterator var21 = registryProcessors.iterator();
   while(var21.hasNext()) {
       String postProcessorName = (String)var21.next();
       orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   invokeBeanFactoryPostProcessors((Collection)orderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
   List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList();
   Iterator var24 = currentRegistryProcessors.iterator();
   while(var24.hasNext()) {
       ppName = (String)var24.next();
       nonOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
   }
   invokeBeanFactoryPostProcessors((Collection)nonOrderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
   // 因為后置處理器可能已經(jīng)修改了原始元數(shù)據(jù),例如,替換值中的占位符
   beanFactory.clearMetadataCache();

到此這篇關(guān)于Spring invokeBeanFactoryPostProcessors方法刨析源碼的文章就介紹到這了,更多相關(guān)Spring invokeBeanFactoryPostProcessors內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring依賴注入多種類型數(shù)據(jù)的示例代碼

    Spring依賴注入多種類型數(shù)據(jù)的示例代碼

    這篇文章主要介紹了Spring依賴注入多種類型數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法

    關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法

    這篇文章主要給大家介紹了關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。
    2017-08-08
  • 詳談jvm線程棧空間內(nèi)存分配位置

    詳談jvm線程棧空間內(nèi)存分配位置

    這篇文章主要介紹了jvm線程??臻g內(nèi)存分配位置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot bean查詢加載順序流程詳解

    SpringBoot bean查詢加載順序流程詳解

    當你在項目啟動時需要提前做一個業(yè)務的初始化工作時,或者你正在開發(fā)某個中間件需要完成自動裝配時。你會聲明自己的Configuration類,但是可能你面對的是好幾個有互相依賴的Bean
    2023-03-03
  • Java中的synchronized鎖膨脹詳解

    Java中的synchronized鎖膨脹詳解

    這篇文章主要介紹了Java中的synchronized鎖膨脹詳解,正常創(chuàng)建的對象,狀態(tài)為無鎖,對象頭的Mark?Word?中主要記錄了?對象的年齡,也就是經(jīng)歷了多少次GC還存活下來,需要的朋友可以參考下
    2024-01-01
  • vue+springboot前后端分離工程跨域問題解決方案解析

    vue+springboot前后端分離工程跨域問題解決方案解析

    這篇文章主要介紹了vue+springboot前后端分離工程跨域問題解決方案解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Mybatis批量操作sql寫法示例(批量新增、更新)

    Mybatis批量操作sql寫法示例(批量新增、更新)

    Mybatis技術(shù),現(xiàn)在是工作中使用頻率越來越高,我們在對數(shù)據(jù)庫進行操作的時候,經(jīng)常會遇到批量操作的需求,這篇文章主要給大家介紹了關(guān)于Mybatis批量操作sql寫法的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • java開發(fā)RocketMQ之NameServer路由管理源碼分析

    java開發(fā)RocketMQ之NameServer路由管理源碼分析

    這篇文章主要為大家介紹了java開發(fā)中RocketMQ之NameServer路由管理源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2021-11-11
  • Mybatis實現(xiàn)分頁的注意點

    Mybatis實現(xiàn)分頁的注意點

    Mybatis提供了強大的分頁攔截實現(xiàn),可以完美的實現(xiàn)分功能。下面小編給大家分享小編在使用攔截器給mybatis進行分頁所遇到的問題及注意點,需要的朋友一起看看吧
    2017-07-07
  • Java并發(fā)編程之線程創(chuàng)建介紹

    Java并發(fā)編程之線程創(chuàng)建介紹

    這篇文章主要介紹了Java并發(fā)編程之線程創(chuàng)建,進程是代碼在數(shù)據(jù)集合上的一次運行活動,是系統(tǒng)進行資源分配和調(diào)度的基本單位,線程則是一個實體,一個進程中至少有一個線程,下文更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04

最新評論