在 ConfigurationClassPostProcessor#processConfigBeanDefinitions
方法中创建了 ConfigurationClassParser
对象并调用其 parse
方法。该方法就是在负责解析配置类、扫描包、注册 BeanDefinition
,源码如下:
//ConfigurationClassParser#parseSet<BeanDefinitionHolder>) 方法源码 public void parse(Set<BeanDefinitionHolder> configCandidates) { for (BeanDefinitionHolder holder : configCandidates) { BeanDefinition bd = holder.getBeanDefinition(); try { // 根据不同的 BeanDefinition 实例对象 调用不同的 parse 方法 // 底层其实都是在调用 org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass if (bd instanceof AnnotatedBeanDefinition) { 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); } } //执行DeferredImportSelector this.deferredImportSelectorHandler.process(); }
在该方法内部根据不同的 BeanDefinition
实例对象,调用了不同的 parse
方法,而这些 parse
方法底层,实际上都是调用了 ConfigurationClassParser#processConfigurationClass
方法。
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException { // 是否需要跳过 @Conditional if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) { return; } // 第一次进入的时候, configurationClasses size = 0,existingClass 肯定为 null 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); this.configurationClasses.put(configClass, configClass); }
方法传入的 ConfigurationClass
对象是对配置类的封装。首先判断配置类上是否有 @Conditional
注解,是否需要跳过解析该配置类。
然后,调用 doProcessConfigurationClass(configClass, sourceClass);
做真正的解析。其中, configClass
是程序的配置类,而 sourceClass
是通过 configClass
创建的。
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException { // @Configuration 继承了 @Component if (configClass.getMetadata().isAnnotated(Component.class.getName())) { // Recursively process any member (nested) classes first // 递归处理内部类 processMemberClasses(configClass, sourceClass); } // Process any @PropertySource annotations // 处理@PropertySource // @PropertySource注解用来加载properties文件 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 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 for (BeanDefinitionHolder holder : scannedBeanDefinitions) { BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition(); if (bdCand == null) { bdCand = holder.getBeanDefinition(); } //判断解析获取的 BeanDefinition 中 是否有配置类 // 这里的配置类包括FullConfigurationClass和LiteConfigurationClass // 也就是说只要有@Configuration、@Component、@ComponentScan、@Import、@ImportResource和@Bean中的其中一个注解 if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) { //如果有配置类,递归调用,解析该配置类,这个if几乎都为true,这个方法几乎都要执行 parse(bdCand.getBeanClassName(), holder.getBeanName()); } } } } // Process any @Import annotations 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 //处理单个@Bean的方法 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; }
配置类上有 @Configuration
注解,该注解继承 @Component
,if 判断为 true
,调用 processMemberClasses
方法,递归解析配置类中的内部类。
@PropertySource
注解 如果配置类上有 @PropertySource
注解,则解析加载 properties
文件,并将属性添加到Spring上下文中。 ((ConfigurableEnvironment) this.environment).getPropertySources().addFirstPropertySource(newSource);
。
@ComponentScan
注解 获取配置类上的 @ComponentScan
注解,判断是否需要跳过。循环所有的ComponentScan,立即执行扫描。 ComponentScanAnnotationParser#parse
方法如下:
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) { // 创建 ClassPathBeanDefinitionScanner // 在 AnnotationConfigApplicationContext 的构造器中也创建了一个ClassPathBeanDefinitionScanner // 这里证明了,执行扫描 scanner 不是构造器中的,而是这里创建的 ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader); // @ComponentScan 中可以注册自定义的 BeanNameGenerator // 但是需要注意,通过源码可以明白,这里注册的自定义BeanNameGenerator 只对当前 scanner 有效 Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator"); boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass); scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator : BeanUtils.instantiateClass(generatorClass)); ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy"); if (scopedProxyMode != ScopedProxyMode.DEFAULT) { scanner.setScopedProxyMode(scopedProxyMode); } else { Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver"); scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass)); } scanner.setResourcePattern(componentScan.getString("resourcePattern")); for (AnnotationAttributes filter : componentScan.getAnnotationArray("includeFilters")) { for (TypeFilter typeFilter : typeFiltersFor(filter)) { scanner.addIncludeFilter(typeFilter); } } for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) { for (TypeFilter typeFilter : typeFiltersFor(filter)) { scanner.addExcludeFilter(typeFilter); } } boolean lazyInit = componentScan.getBoolean("lazyInit"); if (lazyInit) { scanner.getBeanDefinitionDefaults().setLazyInit(true); } Set<String> basePackages = new LinkedHashSet<>(); String[] basePackagesArray = componentScan.getStringArray("basePackages"); for (String pkg : basePackagesArray) { String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg), ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); Collections.addAll(basePackages, tokenized); } // @ComponentScan(basePackageClasses = Xx.class) // 可以指定basePackageClasses, 只要是与是这几个类所在包及其子包,就可以被Spring扫描 // 经常会用一个空的类来作为basePackageClasses,默认取当前配置类所在包及其子包 for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) { basePackages.add(ClassUtils.getPackageName(clazz)); } if (basePackages.isEmpty()) { basePackages.add(ClassUtils.getPackageName(declaringClass)); } scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) { @Override protected boolean matchClassName(String className) { return declaringClass.equals(className); } }); //执行扫描 return scanner.doScan(StringUtils.toStringArray(basePackages)); }
挑一些我觉得是重点的地方记录一下:
parse
方法中新创建了一个 ClassPathBeanDefinitionScanner
对象,而在 AnnotationConfigApplicationContext
的构造器中也创建了一个 ClassPathBeanDefinitionScanner
对象,这里证实了在Spring内部,真正执行扫描的不是 AnnotationConfigApplicationContext
中的scanner。 @ComponentScan
中是可以注册自定义的 BeanNameGenerator
的,而这个 BeanNameGenerator
只对当前scanner有效。也就是说,这个 BeanNameGenerator
只能影响通过该scanner扫描的路径下的bean的BeanName生成规则。 scanner.doScan(StringUtils.toStringArray(basePackages));
方法执行真正的扫描,方法返回扫描获取到的 BeanDefinition
。 检验扫描获得的BeanDefinition中是否有配置类,如果有配置类,这里的配置类包括FullConfigurationClass和LiteConfigurationClass。(也就是说只要有 @Configuration
、 @Component
、 @ComponentScan
、 @Import
、 @ImportResource
和 @Bean
中的其中一个注解),则递归调用 parse
方法,进行解析。
processImports(configClass, sourceClass, getImports(sourceClass), true);
processImports
方法负责对 @Import
注解进行解析。 configClass
是配置类, sourceClass
又是通过 configClass
创建的, getImports(sourceClass)
从 sourceClass
获取所有的 @Import
注解信息,然后调用 ConfigurationClassParser#processImports
。
// ConfigurationClassParser#processImports 源码 private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass, Collection<SourceClass> importCandidates, boolean checkForCircularImports) { if (importCandidates.isEmpty()) { return; } if (checkForCircularImports && isChainedImportOnStack(configClass)) { this.problemReporter.error(new CircularImportProblem(configClass, this.importStack)); } else { this.importStack.push(configClass); try { // importCandidates是@Import的封装 // 循环importCandidates对import的内容进行分类 for (SourceClass candidate : importCandidates) { // import导入实现ImportSelector接口的类 if (candidate.isAssignable(ImportSelector.class)) { // Candidate class is an ImportSelector -> delegate to it to determine imports Class<?> candidateClass = candidate.loadClass(); // 反射创建这个类的实例对象 ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class); //是否有实现相关Aware接口,如果有,这调用相关方法 ParserStrategyUtils.invokeAwareMethods( selector, this.environment, this.resourceLoader, this.registry); // 延迟加载的ImportSelector if (selector instanceof DeferredImportSelector) { // 延迟加载的ImportSelector先放到List中,延迟加载 this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector); } else { // 普通的ImportSelector ,执行其selectImports方法,获取需要导入的类的全限定类名数组 String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata()); Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames); // 递归调用 processImports(configClass, currentSourceClass, importSourceClasses, false); } // 是否为ImportBeanDefinitionRegistrar } else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) { // Candidate class is an ImportBeanDefinitionRegistrar -> // delegate to it to register additional bean definitions Class<?> candidateClass = candidate.loadClass(); ImportBeanDefinitionRegistrar registrar = BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class); ParserStrategyUtils.invokeAwareMethods( registrar, this.environment, this.resourceLoader, this.registry); // 添加到成员变量 org.springframework.context.annotation.ConfigurationClass.importBeanDefinitionRegistrars 中 configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata()); } else { // Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar -> // process it as an @Configuration class // 普通 @Configuration class this.importStack.registerImport( currentSourceClass.getMetadata(), candidate.getMetadata().getClassName()); // 解析导入的@Configuration class processConfigurationClass(candidate.asConfigClass(configClass)); } } } catch (BeanDefinitionStoreException ex) { throw ex; } catch (Throwable ex) { throw new BeanDefinitionStoreException( "Failed to process import candidates for configuration class [" + configClass.getMetadata().getClassName() + "]", ex); } finally { this.importStack.pop(); } } }
@ImportResource
注解可以导入xml配置文件。
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); } }
Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass); for (MethodMetadata methodMetadata : beanMethods) { configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass)); }
将 @Bean
方法转化为 BeanMethod
对象,添加到 ConfigurationClass#beanMethods
集合中。
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(); } }
如果有父类则返回父类Class对象,继续调用该方法。直到返回null,外层循环结束。
do { // 真正的做解析 sourceClass = doProcessConfigurationClass(configClass, sourceClass); } while (sourceClass != null);
源码学习笔记: https://github.com/shenjianen...
欢迎各位关注公众号。