上一篇:Spring5 源碼分析-容器刷新-prepareBeanFactory()方法 java
1.實例化ConfigurationClassPostProcessor,並調用ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry方法完成對配置類的解析,並對解析結果進行對應動做的執行,好比
1.1處理配置類的嵌套類的配置(這是一個遞歸的操做)
1.2處理PropertySource
1.3處理@ComponentScan
1.4執行@Import
1.5執行@ImportSource
1.6執行@Beanapp
2.添加ImportAwareBeanPostProcessor源碼分析
3.實例化EventListenerMethodPostProcessorpost
這個方法幾乎一直在幹同一種事情,也就是找到對應的PostProcessor進行調用,包含BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor兩種類型。spa
BeanDefinitionRegistryPostProcessor:用於註冊BeanDefinition,它的典型實現類有ConfigurationClassPostProcessor.net
BeanFactoryPostProcessor:用於獲取或者修改容器中的BeanDefinition等rest
在分別對兩種類型的PostProcessor進行處理的時候都是按照「PriorityOrder->Order->二者都不是」順序執行的,一旦被執行過的PostProcessor在後面就不會被執行了。code
注:裏面的排序操做由在registerAnnotationConfigProcessors中添加的AnnotationAwareOrderComparator比較器完成,該比較器是PriorityOrder優先於Order,也就是說兩個對象一個是PriorityOrder,一個是Order,則PriorityOrder大,返回1,若是兩個對象都是同一個類型,則根據屬性值大小進行判斷。對象
public static void invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. Set<String> processedBeans = new HashSet<>(); if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryProcessor.postProcessBeanDefinitionRegistry(registry); registryProcessors.add(registryProcessor); } else { regularPostProcessors.add(postProcessor); } } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. // 分三步調用BeanDefinitionRegistryPostProcessors /** * ###################### first開始 ######################### * 首先,調用實現了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor,默認是ConfigurationClassPostProcessor,並實例化默認是ConfigurationClassPostProcessor * ——ConfigurationClassPostProcessor 作了不少事情,根據@ComponentScan掃描和解析處理@Component @Service等類型,以及@Bean @Import @ImportResource 等類型 * ########################################################## */ String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { //getBean會將ConfigurationClassPostProcessor放到容器中,這個Processor會處理使用@Configuration註解的類 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } //根據@Order @Priority註解對Processor進行排序 ,使用到了AnnotationAwareOrderComparator實現類進行排序 sortPostProcessors(currentRegistryProcessors, beanFactory); //將進行了排序的Processors放到後面要使用的registryProcessors中 registryProcessors.addAll(currentRegistryProcessors); // 第一次PostProcessor // 將使用@Configuration註解的類(SpringAPPConfig),根據它的註解功能掃描符合Spring規則的類並放到beanDefinitionMap中,還有屬性文件內容 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); /** * ###################### first 結束 ######################### */ // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { //第一次PostProcessor已經處理,在這裏就不會再處理了,由於processedBeans已經包含了,那麼processedBeans.contains(ppName)則會爲true,下面的判斷就會爲false if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); // 第二次PostProcessor invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { //處理前兩次PostProcessor都沒有處理的PostProcessor if (!processedBeans.contains(ppName)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); reiterate = true; } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); // 第三次PostProcessor invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); } // Now, invoke the postProcessBeanFactory callback of all processors handled so far. //調用到目前爲止全部已經準備(處理過的)好的BeanFactoryPostProcess的callback,好比默認的ConfigurationClassPostProcessor會在這裏加載 // 一個ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor // 第四次PostProcessor invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); // 第五次PostProcessor invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } /*########################################################################################################## * 分三步調用BeanFactoryPostproccesor * */ // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); List<String> orderedPostProcessorNames = new ArrayList<>(); List<String> nonOrderedPostProcessorNames = new ArrayList<>(); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // skip - already processed in first phase above } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. sortPostProcessors(priorityOrderedPostProcessors, beanFactory); // 第六次PostProcessor 調用BeanFactoryPostProcessor invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size()); for (String postProcessorName : orderedPostProcessorNames) { //實例化BeanFactoryPostProcessor orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(orderedPostProcessors, beanFactory); // 第七次PostProcessor 調用BeanFactoryPostProcessor invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. // 調用自定義的BeanFactoryPostProcessor和Spring 非實現Order和Priority接口的BeanFactoryPostProcessor List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size()); for (String postProcessorName : nonOrderedPostProcessorNames) { //實例化BeanFactoryPostProcessor nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } // 第八次PostProcessor 調用BeanFactoryPostProcessor invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // Clear cached merged bean definitions since the post-processors might have // modified the original metadata, e.g. replacing placeholders in values... beanFactory.clearMetadataCache(); }
在本方法中Spring提供了兩中擴展點,點擊對應連接跳轉到介紹的地方blog