你的贊,是我最大的動力。期待與你們一塊兒,共同進步。java
以前的文章介紹到了,AbstractApplicationContext.refresh()
方法,在該方法中有十幾個流程,上一篇文章中介紹了refresh()
的前四個流程。這篇文章將重點介紹invokeBeanFactoryPostProcessors(beanFactory)
。這個 代碼是重點流程,這篇文章詳細分析一下。程序員
第①步:AbstractApplicationContext#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)web
第②步:PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());spring
2.1 registryProcessor.postProcessBeanDefinitionRegistry(registry) 處理自定義的 BeanDefinitionRegistryPostProcessor子類
2.2 DefaultListableBeanFactory.getBeanNamesForType(java.lang.Class<?>, boolean, boolean) 經過type獲得 獲得一個Ben的名稱
複製代碼
第③步:PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors() spring內部本身實現了BeanDefinitionRegistryPostProcessor接口app
第④步:postProcessor.postProcessBeanDefinitionRegistry(registry) 不一樣的子類去本身的實現類中處理,在這裏,spring內部的目前爲止只有一個實現, 那就是ConfigurationClassPostProcessor類,該類是spring內置的。編輯器
第⑤步:ConfigurationClassPostProcessor#processConfigBeanDefinitions(),處理 ConfigurationClassPostProcessoride
第⑥步:ConfigurationClassUtils.checkConfigurationClassCandidate()post
第⑦步:ConfigurationClassParser實例化this
第⑧步:ConfigurationClassParser#parse(Set)url
注:這裏的parse()方法及其重要,下一篇文章詳細介紹,這裏先看一下前面幾步涉及的代碼邏輯!!!
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
/** * getBeanFactoryPostProcessors() 獲取自定義的(咱們本身實現,且沒有交給spring管理的) */ 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())); } 複製代碼
這裏本身定義的 BeanFactoryPostProcessor
的實現,是指是這個類的子類,可是沒有交給spring管理,示例以下:
public class TestFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { int count = beanFactory.getBeanDefinitionCount(); String[] names = beanFactory.getBeanDefinitionNames(); System.out.println("當前BeanFactory中有"+count+" 個Bean"); System.out.println(Arrays.asList(names)); } } 複製代碼
這種方式實現的方式,會經過getBeanFactoryPostProcessors()
的方式拿到。
public static void invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. Set<String> processedBeans = new HashSet<>(); /** 對 BeanDefinitionRegistry 類型的處理 */ if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); /** * BeanDefinitionRegistryPostProcessor */ List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); /**自定義的BeanFactoryPostProcessor*/ for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; /** * 對於 BeanDefinitionRegistryPostProcessor 類型, * 在 BeanFactoryPostProcessor 的基礎上還有本身定義的方法 * 須要先調用 */ registryProcessor.postProcessBeanDefinitionRegistry(registry); registryProcessors.add(registryProcessor); } else { /** * 記錄常規的 BeanFactoryPostProcessor */ regularPostProcessors.add(postProcessor); } } /** * currentRegistryProcessors是放的spring內部本身實現了BeanDefinitionRegistryPostProcessor接口 */ List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. /** * getBeanNamesForType() 經過type 類型 獲得一個Ben的名稱、type指的是 spring bean 描述文件的class類型 */ String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); /** * 這個BeanFactory是spring最開始默認註冊的 */ for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } /** * 排序 */ sortPostProcessors(currentRegistryProcessors, beanFactory); /** * 合併list */ registryProcessors.addAll(currentRegistryProcessors); /** * spring 中 不管是本身定義的 仍是內置的 BeanDefinitionRegistryPostProcessor * 都在這裏處理完成,好比 {@link ConfigurationClassPostProcessor} 的處理 * 這裏是重要代碼。。。 */ invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); /** 清除list */ currentRegistryProcessors.clear(); // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { 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); 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) { if (!processedBeans.contains(ppName)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); reiterate = true; } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); } /** * 執行 BeanFactoryPostProcessor 的回調 * 這裏執行的是 BeanFactoryPostProcessor 的子類 BeanDefinitionRegistryPostProcessor 的回調方法 * */ invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); /** * 這裏執行的是 BeanFactoryPostProcessor 的 postProcessBeanFactory() 方法 */ invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } // 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); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(orderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } 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(); } 複製代碼
private static void invokeBeanDefinitionRegistryPostProcessors( Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) { /** * 循環全部的 BeanDefinitionRegistryPostProcessor */ for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) { /** * 根據不一樣的 BeanDefinitionRegistryPostProcessors 實現 * 去調用不一樣的 postProcessBeanDefinitionRegistry 方法 */ postProcessor.postProcessBeanDefinitionRegistry(registry); } } 複製代碼
經過上述方法,可只在Spring 中先處理程序員本身定義的 BeanFactoryPostProcessor
,而後在處理Spring中內置的,後者說交給其管理的 BeanDefinitionRegistryPostProcessor
,其中 BeanDefinitionRegistryPostProcessor
是 BeanFactoryPostProcessor
的子類。截止到目前爲止 Spring 中惟一的一個內置的 BeanDefinitionRegistryPostProcessor
的實現類就是 ConfigurationClassPostProcessor
接下來就是對 ConfigurationClassPostProcessor
的處理。
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
int registryId = System.identityHashCode(registry); if (this.registriesPostProcessed.contains(registryId)) { throw new IllegalStateException( "postProcessBeanDefinitionRegistry already called on this post-processor against " + registry); } if (this.factoriesPostProcessed.contains(registryId)) { throw new IllegalStateException( "postProcessBeanFactory already called on this post-processor against " + registry); } this.registriesPostProcessed.add(registryId); /** * 處理 ConfigurationClassPostProcessor */ processConfigBeanDefinitions(registry); } 複製代碼
對 ConfigurationClassPostProcessor
的真正的處理是經過 processConfigBeanDefinitions
來完成的
public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
/** 存放spring 中 bean的描述文件*/ List<BeanDefinitionHolder> configCandidates = new ArrayList<>(); /** 獲取容器中全部註冊bean的名字*/ String[] candidateNames = registry.getBeanDefinitionNames(); for (String beanName : candidateNames) { /** 根據beanName 獲取bean的描述文件 */ BeanDefinition beanDef = registry.getBeanDefinition(beanName); if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) || ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { if (logger.isDebugEnabled()) { logger.debug("Bean definition has already been processed as a configuration class: " + beanDef); } } /** * 判斷是不是Configuration類 是否包含 @Configuration 註解 * checkConfigurationClassCandidate()注意這個方法 */ else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) { /** 這裏若是添加了 @Configuration * 將該類封裝成BeanDefinitionHolder 放入到 configCandidates * 後面解析會用到 */ configCandidates.add(new BeanDefinitionHolder(beanDef, beanName)); } } // Return immediately if no @Configuration classes were found if (configCandidates.isEmpty()) { return; } // Sort by previously determined @Order value, if applicable configCandidates.sort((bd1, bd2) -> { int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition()); int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition()); return Integer.compare(i1, i2); }); // Detect any custom bean name generation strategy supplied through the enclosing application context SingletonBeanRegistry sbr = null; if (registry instanceof SingletonBeanRegistry) { sbr = (SingletonBeanRegistry) registry; if (!this.localBeanNameGeneratorSet) { BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR); if (generator != null) { this.componentScanBeanNameGenerator = generator; this.importBeanNameGenerator = generator; } } } if (this.environment == null) { this.environment = new StandardEnvironment(); } // Parse each @Configuration class /** 實例化 ConfigurationClassParser 解析各個配置*/ ConfigurationClassParser parser = new ConfigurationClassParser( this.metadataReaderFactory, this.problemReporter, this.environment, this.resourceLoader, this.componentScanBeanNameGenerator, registry); /** * 實例化兩個Set candidates用於將以前加入的configCandidates 進行去重 * 由於可能有多個配置類重複了 * alreadyParsed 用於判斷是否處理過 */ Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates); Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size()); do { /** * 解析註解類, * 這裏是重點代碼 */ parser.parse(candidates); parser.validate(); Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses()); configClasses.removeAll(alreadyParsed); // Read the model and create bean definitions based on its content if (this.reader == null) { this.reader = new ConfigurationClassBeanDefinitionReader( registry, this.sourceExtractor, this.resourceLoader, this.environment, this.importBeanNameGenerator, parser.getImportRegistry()); } /** * loadBeanDefinitions() 中處理 @Import 的類 */ this.reader.loadBeanDefinitions(configClasses); alreadyParsed.addAll(configClasses); candidates.clear(); if (registry.getBeanDefinitionCount() > candidateNames.length) { String[] newCandidateNames = registry.getBeanDefinitionNames(); Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames)); Set<String> alreadyParsedClasses = new HashSet<>(); for (ConfigurationClass configurationClass : alreadyParsed) { alreadyParsedClasses.add(configurationClass.getMetadata().getClassName()); } for (String candidateName : newCandidateNames) { if (!oldCandidateNames.contains(candidateName)) { BeanDefinition bd = registry.getBeanDefinition(candidateName); if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) && !alreadyParsedClasses.contains(bd.getBeanClassName())) { candidates.add(new BeanDefinitionHolder(bd, candidateName)); } } } candidateNames = newCandidateNames; } } while (!candidates.isEmpty()); // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) { sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry()); } if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it'll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } } 複製代碼
對於 beanDef
判斷的代碼片斷以下:
BeanDefinition beanDef = registry.getBeanDefinition(beanName);
if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) || ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { if (logger.isDebugEnabled()) { logger.debug("Bean definition has already been processed as a configuration class: " + beanDef); } } 複製代碼
在 ConfigurationClassUtils
中定義了兩個常量用 private static final String CONFIGURATION_CLASS_FULL = "full"
和 private static final String CONFIGURATION_CLASS_LITE = "lite"
使用這兩個常量用來判斷當前的 BeanDefinition
是一個 全配置類仍是 部分配置類。在這裏對於 @Configuration
註解的配置裏走下面的 checkConfigurationClassCandidate
判斷,可斷點調試驗證。
public static boolean checkConfigurationClassCandidate( BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) { String className = beanDef.getBeanClassName(); if (className == null || beanDef.getFactoryMethodName() != null) { return false; } AnnotationMetadata metadata; if (beanDef instanceof AnnotatedBeanDefinition && className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) { /** * 若是 beanDefinition 是 AnnotatedBeanDefinition的實例 * 而且 className 和 beanDefinition 中的元數據類名相同 * 從 AnnotatedBeanDefinition 中獲取 元數據 */ // Can reuse the pre-parsed metadata from the given BeanDefinition... metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata(); } else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) { // Check already loaded Class if present... // since we possibly can't even load the class file for this Class. /** * 若是 beanDefinition 是 AbstractBeanDefinition 的實例 * 而且 beanDefinition 有 beanClass屬性存在 * 實例化 StandardAnnotationMetadata */ Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass(); metadata = new StandardAnnotationMetadata(beanClass, true); } else { try { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className); metadata = metadataReader.getAnnotationMetadata(); } catch (IOException ex) { if (logger.isDebugEnabled()) { logger.debug("Could not find class file for introspecting configuration annotations: " + className, ex); } return false; } } /** 判斷元數據 是否加了 @Configuration 註解*/ if (isFullConfigurationCandidate(metadata)) { /** * 若是存在 @Configuration 註解,beanDefinition 中設置 configurationClass 爲 full * spring 認爲該類是一個全註解的類 */ beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL); } /** * 判斷是否加了 @Import,@Component * @ImportResource, @ComponentScan 註解 */ else if (isLiteConfigurationCandidate(metadata)) { /** * 若是不存在 @Configuration 註解,beanDefinition 中設置 configurationClass 爲 lite * spring 認爲該類是一個部分註解類 */ beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE); } else { return false; } // It's a full or lite configuration candidate... Let's determine the order value, if any. Integer order = getOrder(metadata); if (order != null) { beanDef.setAttribute(ORDER_ATTRIBUTE, order); } return true; } 複製代碼
在 checkConfigurationClassCandidate
方法中 @Configuration
註解是的類是 AnnotatedBeanDefinition
的實現,首先獲取到元數據 metadata
以下圖: 而後,對於加了
@Configuration
註解的類,會將該類的屬性設置爲 full
,以下圖: 最後,若是
checkConfigurationClassCandidate()
方法返回true,而後將 BeanDefinition
封裝成 BeanDefinitionHolder
添加到 configCandidates
中,供後面解析使用。
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment, this.resourceLoader, this.componentScanBeanNameGenerator, registry); 複製代碼
在本文章中並無在 IoC容器中添加新的對象。這篇文章中涉及到的就是對
@Configuration
註解類對應的Spring內置的ConfigurationClassPostProcessor
處理。
本文使用 mdnice 排版