最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?invokeBeanFactoryPostProcessors方法刨析源碼

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

概述

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

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

invokeBeanFactoryPostProcessors

	/**
	 * 實(shí)例化并且調(diào)用所有已經(jīng)注冊(cè)了的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) {
		// 獲取到當(dāng)前應(yīng)用程序上下文的beanFactoryPostProcessors變量的值,并且實(shí)例化調(diào)用執(zhí)行所有已經(jīng)注冊(cè)的beanFactoryPostProcessor
		// 默認(rèn)情況下,通過getBeanFactoryPostProcessors()來獲取已經(jīng)注冊(cè)的BFPP,但是默認(rèn)是空的 
		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方法,默認(rèn)為空。

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

invokeBeanFactoryPostProcessors方法

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory,

List beanFactoryPostProcessors){}

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

是DefaultListableBeanFactory 類型,DefaultListableBeanFactory類實(shí)現(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;
	}

所以會(huì)走下面分支

if (beanFactory instanceof BeanDefinitionRegistry) {
...
}

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

invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);

BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor分類

  // 類型轉(zhuǎn)換
  BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
  // 此處希望大家做一個(gè)區(qū)分,兩個(gè)接口是不同的,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子集
  // BeanFactoryPostProcessor主要針對(duì)的操作對(duì)象是BeanFactory,而BeanDefinitionRegistryPostProcessor主要針對(duì)的操作對(duì)象是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);
      }
  }

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

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

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

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

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

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

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

處理BeanFactory容器中注冊(cè)的BeanFactoryPostProcessor類

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

   // 到這里為止,入?yún)eanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已經(jīng)全部處理完畢,下面開始處理容器中
   // 所有的BeanFactoryPostProcessor
   // 可能會(huì)包含一些實(shí)現(xiàn)類,只實(shí)現(xiàn)了BeanFactoryPostProcessor,并沒有實(shí)現(xiàn)BeanDefinitionRegistryPostProcessor接口,
   // 因?yàn)橛衟rocessedBeans記錄了上面處理的實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor的類,所以不會(huì)重復(fù)處理。
   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);
   // 因?yàn)楹笾锰幚砥骺赡芤呀?jīng)修改了原始元數(shù)據(jù),例如,替換值中的占位符
   beanFactory.clearMetadataCache();

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

相關(guān)文章

  • 一起來學(xué)習(xí)JAVA的運(yùn)算符

    一起來學(xué)習(xí)JAVA的運(yùn)算符

    這篇文章主要為大家詳細(xì)介紹了JAVA的運(yùn)算符,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java讀取Properties配置文件的6種方式匯總

    Java讀取Properties配置文件的6種方式匯總

    這篇文章主要給大家介紹了關(guān)于Java讀取Properties配置文件的6種方式,java中的properties文件是一種配置文件,主要用于表達(dá)配置信息,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • SpringCloud turbine監(jiān)控實(shí)現(xiàn)過程解析

    SpringCloud turbine監(jiān)控實(shí)現(xiàn)過程解析

    這篇文章主要介紹了SpringCloud turbine監(jiān)控實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • JVM中最耗cpu的線程查找方法

    JVM中最耗cpu的線程查找方法

    這篇文章主要為大家介紹了JVM中最耗cpu的線程查找方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • SpringBoot如何利用Twilio?Verify發(fā)送驗(yàn)證碼短信

    SpringBoot如何利用Twilio?Verify發(fā)送驗(yàn)證碼短信

    Twilio提供了一個(gè)名為?Twilio?Verify?的服務(wù),專門用于處理驗(yàn)證碼的發(fā)送和驗(yàn)證,下面我們就來看看如何使用Twilio?Verify實(shí)現(xiàn)發(fā)送驗(yàn)證碼短信吧
    2025-03-03
  • Mybatis?sql與xml文件讀取方法詳細(xì)分析

    Mybatis?sql與xml文件讀取方法詳細(xì)分析

    這篇文章主要介紹了Mybatis?sql與xml文件讀取方法,在執(zhí)行一個(gè)自定義sql語句時(shí),dao對(duì)應(yīng)的代理對(duì)象時(shí)如何找到sql,也就是dao的代理對(duì)象和sql之間的關(guān)聯(lián)關(guān)系是如何建立的
    2023-01-01
  • Java使用modbus-master-tcp實(shí)現(xiàn)modbus tcp通訊

    Java使用modbus-master-tcp實(shí)現(xiàn)modbus tcp通訊

    這篇文章主要為大家詳細(xì)介紹了另外一種Java語言的modbux tcp通訊方案,那就是modbus-master-tcp,文中的示例代碼講解詳細(xì),需要的可以了解下
    2023-12-12
  • 利用Java+Selenium+OpenCV模擬實(shí)現(xiàn)網(wǎng)頁滑動(dòng)驗(yàn)證

    利用Java+Selenium+OpenCV模擬實(shí)現(xiàn)網(wǎng)頁滑動(dòng)驗(yàn)證

    目前很多網(wǎng)頁都有滑動(dòng)驗(yàn)證,目的就是防止不良爬蟲扒他們網(wǎng)站的數(shù)據(jù)。本文將介紹通過Java Selenium OpenCV解決網(wǎng)頁滑塊驗(yàn)證,需要的可以參考一下
    2022-01-01
  • Java優(yōu)化重復(fù)冗余代碼的8種方式總結(jié)

    Java優(yōu)化重復(fù)冗余代碼的8種方式總結(jié)

    日常開發(fā)中,我們經(jīng)常會(huì)遇到一些重復(fù)代碼,最近小編優(yōu)化了一些系統(tǒng)中的重復(fù)代碼,用了好幾種的方式,感覺挺有用的,所以本文給大家講講優(yōu)化重復(fù)代碼的幾種方式
    2023-08-08
  • Java文件字符輸入流FileReader讀取txt文件亂碼的解決

    Java文件字符輸入流FileReader讀取txt文件亂碼的解決

    這篇文章主要介紹了Java文件字符輸入流FileReader讀取txt文件亂碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論

盐津县| 龙岩市| 璧山县| 藁城市| 双城市| 连州市| 玉树县| 巴彦淖尔市| 英超| 北安市| 阜城县| 莫力| 兴城市| 宝坻区| 梁河县| 开封县| 长治市| 乌兰县| 安仁县| 梨树县| 若尔盖县| 辰溪县| 金坛市| 米林县| 丁青县| 林州市| 辽源市| 资中县| 张家口市| 锦屏县| 商河县| 五寨县| 阳山县| 江源县| 黄石市| 隆化县| 望城县| 盐池县| 阿鲁科尔沁旗| 汉阴县| 兴宁市|