Spring?invokeBeanFactoryPostProcessors方法刨析源碼
概述
invokeBeanFactoryPostProcessor方法是spring核心方法之一,主要用來(lái)調(diào)用beanFactory后置處理器來(lái)修改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)情況下,通過(guò)getBeanFactoryPostProcessors()來(lái)獲取已經(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 類(lèi)型,DefaultListableBeanFactory類(lèi)實(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分類(lèi)
// 類(lèi)型轉(zhuǎn)換 BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; // 此處希望大家做一個(gè)區(qū)分,兩個(gè)接口是不同的,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子集 // BeanFactoryPostProcessor主要針對(duì)的操作對(duì)象是BeanFactory,而B(niǎo)eanDefinitionRegistryPostProcessor主要針對(duì)的操作對(duì)象是BeanDefinition // 存放BeanFactoryPostProcessor的集合 List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); // 存放BeanDefinitionRegistryPostProcessor的集合 List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); // 首先處理入?yún)⒅械腷eanFactoryPostProcessors,遍歷所有的beanFactoryPostProcessors,將BeanDefinitionRegistryPostProcessor // 和BeanFactoryPostProcessor區(qū)分開(kāi) 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類(lèi)
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. // 調(diào)用所有實(shí)現(xiàn)PriorityOrdered接口的BeanDefinitionRegistryPostProcessor實(shí)現(xiàn)類(lèi) // 找到所有實(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名稱(chēng)添加到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類(lèi)
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { // 檢測(cè)是否實(shí)現(xiàn)了Ordered接口,并且還未執(zhí)行過(guò) if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { // 獲取名字對(duì)應(yīng)的bean實(shí)例,添加到currentRegistryProcessors中 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); // 將要被執(zhí)行的BFPP名稱(chēng)添加到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類(lèi)
// 最后,調(diào)用所有剩下的BeanDefinitionRegistryPostProcessors boolean reiterate = true; while (reiterate) { reiterate = false; // 找出所有實(shí)現(xiàn)BeanDefinitionRegistryPostProcessor接口的類(lèi) postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); // 遍歷執(zhí)行 for (String ppName : postProcessorNames) { // 跳過(guò)已經(jīng)執(zhí)行過(guò)的BeanDefinitionRegistryPostProcessor if (!processedBeans.contains(ppName)) { // 獲取名字對(duì)應(yīng)的bean實(shí)例,添加到currentRegistryProcessors中 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); // 將要被執(zhí)行的BFPP名稱(chēng)添加到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類(lèi)
處理方式與上面處理BeanDefinitionRegistryPostProcessor類(lèi)似,按照先后順序分別處理實(shí)現(xiàn)了PriorityOrdered接口、Ordered接口、沒(méi)有實(shí)現(xiàn)Ordered接口的bean。這里不在詳細(xì)說(shuō)明。
// 到這里為止,入?yún)eanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已經(jīng)全部處理完畢,下面開(kāi)始處理容器中 // 所有的BeanFactoryPostProcessor // 可能會(huì)包含一些實(shí)現(xiàn)類(lèi),只實(shí)現(xiàn)了BeanFactoryPostProcessor,并沒(méi)有實(shí)現(xiàn)BeanDefinitionRegistryPostProcessor接口, // 因?yàn)橛衟rocessedBeans記錄了上面處理的實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor的類(lèi),所以不會(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)文章
Spring依賴(lài)注入多種類(lèi)型數(shù)據(jù)的示例代碼
這篇文章主要介紹了Spring依賴(lài)注入多種類(lèi)型數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08SpringBoot bean查詢(xún)加載順序流程詳解
當(dāng)你在項(xiàng)目啟動(dòng)時(shí)需要提前做一個(gè)業(yè)務(wù)的初始化工作時(shí),或者你正在開(kāi)發(fā)某個(gè)中間件需要完成自動(dòng)裝配時(shí)。你會(huì)聲明自己的Configuration類(lèi),但是可能你面對(duì)的是好幾個(gè)有互相依賴(lài)的Bean2023-03-03vue+springboot前后端分離工程跨域問(wèn)題解決方案解析
這篇文章主要介紹了vue+springboot前后端分離工程跨域問(wèn)題解決方案解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Mybatis批量操作sql寫(xiě)法示例(批量新增、更新)
Mybatis技術(shù),現(xiàn)在是工作中使用頻率越來(lái)越高,我們?cè)趯?duì)數(shù)據(jù)庫(kù)進(jìn)行操作的時(shí)候,經(jīng)常會(huì)遇到批量操作的需求,這篇文章主要給大家介紹了關(guān)于Mybatis批量操作sql寫(xiě)法的相關(guān)資料,需要的朋友可以參考下2021-05-05java開(kāi)發(fā)RocketMQ之NameServer路由管理源碼分析
這篇文章主要為大家介紹了java開(kāi)發(fā)中RocketMQ之NameServer路由管理源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11Mybatis實(shí)現(xiàn)分頁(yè)的注意點(diǎn)
Mybatis提供了強(qiáng)大的分頁(yè)攔截實(shí)現(xiàn),可以完美的實(shí)現(xiàn)分功能。下面小編給大家分享小編在使用攔截器給mybatis進(jìn)行分頁(yè)所遇到的問(wèn)題及注意點(diǎn),需要的朋友一起看看吧2017-07-07