Spring?invokeBeanFactoryPostProcessors方法刨析源碼
概述
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ù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法
這篇文章主要給大家介紹了關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。2017-08-08vue+springboot前后端分離工程跨域問題解決方案解析
這篇文章主要介紹了vue+springboot前后端分離工程跨域問題解決方案解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03java開發(fā)RocketMQ之NameServer路由管理源碼分析
這篇文章主要為大家介紹了java開發(fā)中RocketMQ之NameServer路由管理源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2021-11-11