BeanFactory後置處理器 - ConfigurationClassPostProcessor - full / lite
ConfigurationClassPostProcessor
首先看看他的類的關係:
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { }
接著看:
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor{ void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException; }
public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
invokeBeanFactoryPostProcessors方法中看到, spring 是先處理
他剛好都實現了這兩個介面.
按照spring 的執行順序, 首先看postProcessBeanDefinitionRegistry 方法
postProcessBeanDefinitionRegistry
@Override 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); processConfigBeanDefinitions(registry); }
看主要方法processConfigBeanDefinitions
/** * Build and validate a configuration model based on the registry of * {@link Configuration} classes. */ public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) { //定義一個list存放app 提供的bd(專案當中提供了@Compent) List<BeanDefinitionHolder> configCandidates = new ArrayList<>(); //獲取容器中註冊的所有bd名字 //6個 String[] candidateNames = registry.getBeanDefinitionNames(); /** * Full * Lite */ for (String beanName : candidateNames) { BeanDefinition beanDef = registry.getBeanDefinition(beanName); if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) || ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { //果BeanDefinition中的configurationClass屬性為full或者lite,則意味著已經處理過了,直接跳過 //這裡需要結合下面的程式碼才能理解 if (logger.isDebugEnabled()) { logger.debug("Bean definition has already been processed as a configuration class: " + beanDef); } } //判斷是否是Configuration類,如果加了Configuration下面的這幾個註解就不再判斷了 // 還有 add(Component.class.getName()); // candidateIndicators.add(ComponentScan.class.getName()); // candidateIndicators.add(Import.class.getName()); // candidateIndicators.add(ImportResource.class.getName()); //beanDef == startConfig else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) { //BeanDefinitionHolder 也可以看成一個數據結構 configCandidates.add(new BeanDefinitionHolder(beanDef, beanName)); } } // Return immediately if no @Configuration classes were found if (configCandidates.isEmpty()) { return; } // 排序,根據order,不重要 // 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; //如果BeanDefinitionRegistry是SingletonBeanRegistry子類的話, // 由於我們當前傳入的是DefaultListableBeanFactory,是SingletonBeanRegistry 的子類 // 因此會將registry強轉為SingletonBeanRegistry if (registry instanceof SingletonBeanRegistry) { sbr = (SingletonBeanRegistry) registry; if (!this.localBeanNameGeneratorSet) {//是否有自定義的 BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR); //SingletonBeanRegistry中有id為 org.springframework.context.annotation.internalConfigurationBeanNameGenerator //如果有則利用他的,否則則是spring預設的 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); //例項化2個set,candidates用於將之前加入的configCandidates進行去重 //因為可能有多個配置類重複了 //alreadyParsed用於判斷是否處理過 Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates); Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size()); do { parser.parse(candidates); parser.validate(); //map.keyset 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()); } /** * 這裡值得注意的是掃描出來的bean當中可能包含了特殊類 * 比如ImportBeanDefinitionRegistrar那麼也在這個方法裡面處理 * 但是並不是包含在configClasses當中 * configClasses當中主要包含的是importSelector * 因為ImportBeanDefinitionRegistrar在掃描出來的時候已經被新增到一個list當中去了 */ //將 bd 放到 map this.reader.loadBeanDefinitions(configClasses); alreadyParsed.addAll(configClasses); candidates.clear(); //由於我們這裡進行了掃描,把掃描出來的BeanDefinition註冊給了factory 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(); } }
這個方法主要是對配置類進行解析, 例如前面篇幅中的 StartConfig.class
通過解析可能會得到的一批類的定義 BeanDefinition(ComponentScan , Import)
然後將這些 bd , 通過 loadBeanDefinitions 方法, 註冊到spring容器中
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())) { // Can reuse the pre-parsed metadata from the given BeanDefinition... //如果BeanDefinition 是 AnnotatedBeanDefinition的例項,並且className 和 BeanDefinition中 的元資料 的類名相同 // 則直接從BeanDefinition 獲得Metadata 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的例項,並且beanDef 有 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; } } //判斷當前這個bd中存在的類是不是加了@Configruation註解 //如果存在則spring認為他是一個全註解的類 if (isFullConfigurationCandidate(metadata)) { //如果存在Configuration 註解,則為BeanDefinition 設定configurationClass屬性為full beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL); } //判斷是否加了以下註解,摘錄isLiteConfigurationCandidate的原始碼 // candidateIndicators.add(Component.class.getName()); // candidateIndicators.add(ComponentScan.class.getName()); // candidateIndicators.add(Import.class.getName()); // candidateIndicators.add(ImportResource.class.getName()); //如果不存在Configuration註解,spring則認為是一個部分註解類 else if (isLiteConfigurationCandidate(metadata)) { 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; }
這裡有兩個重要的方法:
1. isFullConfigurationCandidate
這個方法, 是判斷配置類上面, 有沒有直接或者間接的加 @Configuration 註解,
如果加了這個註解, 則設定屬性CONFIGURATION_CLASS_ATTRIBUTE = full
public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) { return metadata.isAnnotated(Configuration.class.getName()); }
直接的方式很好理解, 就是
@Configuration public class StartConfig {}
間接的方式, 其實就是 配置類上沒有加 Configuration, 但是它上面加了其他的註解, 並且這個註解中有 Configuration, 如:
@Configuration public @interface EnableIndex {} @EnableIndex public class StartConfig {}
2.isLiteConfigurationCandidate
這個方法相對複雜一點. 注意到前面的原始碼是 if ... else if ...
所以, 如果上面那個方法滿足了, 是不會進這個方法的. 能進這個方法, 則說明, 配置類上, 根本就沒有 @Configuration 配置類.
這裡會先檢測配置類中, 是否有 @Component / @ComponentScan / @Import / @ImportResource
如果都沒有, 則會去檢測配置類中的方法上, 是否有@Bean
如果能滿足以上的條件,則設定屬性CONFIGURATION_CLASS_ATTRIBUTE = lite
private static final Set<String> candidateIndicators = new HashSet<>(8); static { candidateIndicators.add(Component.class.getName()); candidateIndicators.add(ComponentScan.class.getName()); candidateIndicators.add(Import.class.getName()); candidateIndicators.add(ImportResource.class.getName()); } public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) { // Do not consider an interface or an annotation... if (metadata.isInterface()) { return false; } // Any of the typical annotations found? for (String indicator : candidateIndicators) { if (metadata.isAnnotated(indicator)) { return true; } } // Finally, let's look for @Bean methods... try { return metadata.hasAnnotatedMethods(Bean.class.getName()); } catch (Throwable ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to introspect @Bean methods on class [" + metadata.getClassName() + "]: " + ex); } return false; } }
費這麼多程式碼, 得到結果, 設定屬性 CONFIGURATION_CLASS_ATTRIBUTE = full / lite, 到底有什麼意義呢?
先做兩個小測試, 一個是配置類上有 @Configuration 註解, 另一個是配置類上, 沒有 @Configuration 註解.
1. 沒有註解的
public class IndexDao1 { public IndexDao1() { System.out.println("IndexDao1 -- constructor"); } } public class IndexDao2 { public IndexDao2() { System.out.println("IndexDao2 -- constructor"); } }
//@Configuration public class StartConfig { @Bean public IndexDao1 indexDao1(){ return new IndexDao1(); } @Bean public IndexDao2 indexDao2(){ indexDao1(); return new IndexDao2(); } }
通過上面的程式碼, 發現 indexDao2() 方法裡面呼叫了 IndexDao1()方法, 那麼理論上, IndexDao1 是不是會被建立兩遍呢?
通過執行
public static void main(String[] args) { AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(StartConfig.class); IndexDao1 bean1 = acac.getBean(IndexDao1.class); IndexDao2 bean2 = acac.getBean(IndexDao2.class); }
這段測試程式碼, 我發現, IndexDao1 確實被建立了兩遍
2. 有@Configuration註解的
程式碼還是上面那段程式碼, 只是在 StartConfig 上面加一個 @Configuration
執行測試程式碼
則會發現 IndexDao1 只被建立了一次.
這裡的一次和兩次, 和這個 full / lite 有什麼關係呢?
程式碼還沒到, 暫時不揭曉謎底了, 到後面執行
org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory
的時候, 再來揭曉謎底吧.
parser.parse
這個方法內容比較多, 不考慮 xml 的情況, 這裡主要是處理 ComponentScan 和 Import 的.
public void parse(Set<BeanDefinitionHolder> configCandidates) { //根據BeanDefinition 的型別 做不同的處理,一般都會呼叫ConfigurationClassParser#parse 進行解析 for (BeanDefinitionHolder holder : configCandidates) { BeanDefinition bd = holder.getBeanDefinition(); try { if (bd instanceof AnnotatedBeanDefinition) { //解析註解物件,並且把解析出來的bd放到map,但是這裡的 bd 指的是普通的 //何謂不普通的呢?比如@Bean 和各種beanFactoryPostProcessor得到的bean不在這裡put //但是 是這裡解析,只是不put而已 parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName()); } else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) { parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName()); } else { parse(bd.getBeanClassName(), holder.getBeanName()); } } catch (BeanDefinitionStoreException ex) { throw ex; } catch (Throwable ex) { throw new BeanDefinitionStoreException( "Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex); } } //處理延遲載入的importSelect?為什麼要延遲載入,估計就是為了延遲吧 this.deferredImportSelectorHandler.process(); }
parse 方法內部, 會去呼叫org.springframework.context.annotation.ConfigurationClassParser#processConfigurationClass 方法
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException { if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) { return; } // 處理Imported 的情況 //就是當前這個註解類有沒有被別的類import ConfigurationClass existingClass = this.configurationClasses.get(configClass); if (existingClass != null) { if (configClass.isImported()) { if (existingClass.isImported()) { existingClass.mergeImportedBy(configClass); } // Otherwise ignore new imported config class; existing non-imported class overrides it. return; } else { // Explicit bean definition found, probably replacing an import. // Let's remove the old one and go with the new one. this.configurationClasses.remove(configClass); this.knownSuperclasses.values().removeIf(configClass::equals); } } // Recursively process the configuration class and its superclass hierarchy. SourceClass sourceClass = asSourceClass(configClass); do { sourceClass = doProcessConfigurationClass(configClass, sourceClass); } while (sourceClass != null); //一個map,用來存放掃描出來的bean(注意這裡的bean不是物件,僅僅bean的資訊,因為還沒到例項化這一步) this.configurationClasses.put(configClass, configClass); }
在這個方法中, 將配置類封裝成了 SourceClass,
然後呼叫
org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass 方法.
doProcessConfigurationClass 就是要解析的重點方法.
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException { if (configClass.getMetadata().isAnnotated(Component.class.getName())) { // Recursively process any member (nested) classes first //處理內部類, 正常情況下, 我們不會用這個來配置, 所以不用重點關注 processMemberClasses(configClass, sourceClass); } // Process any @PropertySource annotations for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable( sourceClass.getMetadata(), PropertySources.class, org.springframework.context.annotation.PropertySource.class)) { if (this.environment instanceof ConfigurableEnvironment) { processPropertySource(propertySource); } else { logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() + "]. Reason: Environment must implement ConfigurableEnvironment"); } } // Process any @ComponentScan annotations Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable( sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class); if (!componentScans.isEmpty() && !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) { for (AnnotationAttributes componentScan : componentScans) { // The config class is annotated with @ComponentScan -> perform the scan immediately //掃描普通類 //這裡掃描出來所有 @Component //並且把掃描的出來的普通bean放到 map 當中 Set<BeanDefinitionHolder> scannedBeanDefinitions = this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName()); // Check the set of scanned definitions for any further config classes and parse recursively if needed //檢查掃描出來的類當中是否還有configuration for (BeanDefinitionHolder holder : scannedBeanDefinitions) { BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition(); if (bdCand == null) { bdCand = holder.getBeanDefinition(); } //檢查掃描出來的類中, 是否還有載入了 @Configuration 的類, 如果有, 則接著遞迴處理 if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) { parse(bdCand.getBeanClassName(), holder.getBeanName()); } } } }
// Process any @Import annotations // 處理@Import imports 3種情況 // ImportSelector 將類的字串陣列返回給spring, 這些類的建立過程, 完全由 spring 去解析建立, 經典示例: @EnableCaching // 普通類 普通類會解析成 db, 放到 Map<ConfigurationClass, ConfigurationClass> configurationClasses 中,
// 等待parse方法執行完後, 註冊到 spring 容器中, 由 spring 去建立 // ImportBeanDefinitionRegistrar 使用者建立(或掃描建立) bd, 然後將這些bd註冊到容器中, 由spring去建立, 經典示例: mybatis //這裡和內部遞迴呼叫時候的情況不同 /** * 這裡處理的import是需要判斷我們的類當中時候有@Import註解 * 如果有這把@Import當中的值拿出來,是一個類 * 比如@Import(xxxxx.class),那麼這裡便把xxxxx傳進去進行解析 * 在解析的過程中如果發覺是一個importSelector那麼就回調selector的方法 * 返回一個字串(類名),通過這個字串得到一個類 * 繼而在遞迴呼叫本方法來處理這個類 * * 判斷一組類是不是imports(3種import) */ processImports(configClass, sourceClass, getImports(sourceClass), true); // Process any @ImportResource annotations AnnotationAttributes importResource = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class); if (importResource != null) { String[] resources = importResource.getStringArray("locations"); Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader"); for (String resource : resources) { String resolvedResource = this.environment.resolveRequiredPlaceholders(resource); configClass.addImportedResource(resolvedResource, readerClass); } } // Process individual @Bean methods Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass); for (MethodMetadata methodMetadata : beanMethods) { configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass)); } // Process default methods on interfaces processInterfaces(configClass, sourceClass); // Process superclass, if any if (sourceClass.getMetadata().hasSuperClass()) { String superclass = sourceClass.getMetadata().getSuperClassName(); if (superclass != null && !superclass.startsWith("java") && !this.knownSuperclasses.containsKey(superclass)) { this.knownSuperclasses.put(superclass, configClass); // Superclass found, return its annotation metadata and recurse return sourceClass.getSuperClass(); } } // No superclass -> processing is complete return null; }
這裡的程式碼比較複雜, 就不再這一篇解析了, 不然篇幅太長了, 我自己都不想看了.(汗)