前面三节主要讲了如何加载注解Bean的BeanDefinition,执行环节是在DefaultBeanDefinitionDocumentReader.parseBeanDefinitions中用BeanDefinitionParserDelegate.parseCustomElement(ele)加载的,实际上没对注解真正进行解析。本节主要讲述注解bean如何被正确解析。
解析对象
用解析器对含配置类注解的java文件进行解析,例如:
@Configuration
public class AppConfig {@Beanpublic MyServiceImpl myService() {return new MyServiceImpl();}...
}
@Component/@Controller/@Service/@Repository
public class ComponentClass{...
}
解析入口
真正解析应用注解bean是在应用容器刷新时,入口过程:
解析过程
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, List beanFactoryPostProcessors)
BeanFactoryPostProcessor执行过程:遍历执行处理器的postProcessBeanDefinitionRegistry方法。
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {/* 警告:尽管看起来可以很容易地重构此方法的主体,以避免使用多个循环和多个列表,但使用多个列表和多次传递处理器名称是有意的。我们必须确保遵守PriorityOrdered和Ordered处理顺序。具体来说,我们决不能使处理器被实例化或以错误的顺序在applicationContext中注册。*//* 必须优先调用BeanDefinitionRegistryPostProcessors *///存放bean后置处理器名称的集合Set<String> processedBeans = new HashSet<>();//判断传入的beanFactory是否属于BeanDefinitionRegistry类型if (beanFactory instanceof BeanDefinitionRegistry registry) {//创建BeanFactory后置处理器的集合List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();//创建用于注册BeanDefinition后置处理器的集合List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();// 遍历BeanFactory后置处理器for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {if (postProcessor instanceof BeanDefinitionRegistryPostProcessor registryProcessor) {//如果是BeanDefinition的后置处理器// 注册BeanDefinition(执行入口)registryProcessor.postProcessBeanDefinitionRegistry(registry);//加入到注册BeanDefinition集合registryProcessors.add(registryProcessor);}else {//如果不是BeanDefinition的后置处理器,直接加入BeanFactory后置处理器集合regularPostProcessors.add(postProcessor);}}/*这里不要初始化FactoryBeans:我们需要保持所有常规bean未初始化,以便让bean工厂的后处理器统一处理!按顺序实现PriorityOrdered、Ordered和其他的BeanDefinitionRegistryPostProcessors的处理。*/// 再创建一个用于每个步骤用的注册BeanDefinition后置处理器的集合 List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();/* 第一步:优先把PriorityOrdered类型的BeanDefinitionRegistryPostProcessors加入currentRegistryProcessors,并执行解析入口,然后整个加入到registryProcessors */String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);for (String ppName : postProcessorNames) {if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));processedBeans.add(ppName);}}// 排序sortPostProcessors(currentRegistryProcessors, beanFactory);// currentRegistryProcessors中的处理器全部加u到registryProcessorsregistryProcessors.addAll(currentRegistryProcessors);//执行registryProcessors里面所有后处理器的方法-postProcessBeanDefinitionRegistryinvokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());// 清空currentRegistryProcessorscurrentRegistryProcessors.clear();/* 第二步:把Ordered类型的BeanDefinitionRegistryPostProcessors加入currentRegistryProcessors,,并执行解析入口,然后整个加入到registryProcessors (处理方式同“第一步”) */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, beanFactory.getApplicationStartup());currentRegistryProcessors.clear();/* 第三步:把其它(非PriorityOrdered、Ordered类型)的BeanDefinitionRegistryPostProcessors加入currentRegistryProcessors,,并执行解析入口,然后整个加入到registryProcessors (处理方式同“第一步”) */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, beanFactory.getApplicationStartup());currentRegistryProcessors.clear();}/* 调用BeanDefinitionRegistryPostProcessor所有处理器的回调功能:postProcessBeanFactory */invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);/* 调用BeanFactoryPostProcessor所有处理器的回调功能:postProcessBeanFactory */invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);}else {//beanFactory非BeanDefinitionRegistry类型,直接调所有处理器的回调功能:postProcessBeanFactoryinvokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);}/* 这里不要初始化FactoryBeans:我们需要保持所有常规bean未初始化,以便让bean工厂后处理器统一处理!*//* 前面处理不能保证beanFactoryPostProcessors已全部处理,再处理一次 */String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);/* 把所有的BeanFactoryPostProcessor分成三类:PriorityOrdered、Ordered和其它 */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// 如果processedBeans已经包含处理器名字,表示已处理,跳过}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);}}/* 第一步:处理priorityOrderedPostProcessors:排序,并调用回调功能-postProcessBeanFactory */sortPostProcessors(priorityOrderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);/* 第二步:处理orderedPostProcessors:转处理器名为bean,排序,并调用回调功能-postProcessBeanFactory */List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());for (String postProcessorName : orderedPostProcessorNames) {orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));}sortPostProcessors(orderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);/* 第三步:处理其它nonOrderedPostProcessors:转处理器名为bean,不需排序,并调用回调功能-postProcessBeanFactory */List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());for (String postProcessorName : nonOrderedPostProcessorNames) {nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));}invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);/* 清除缓存的合并bean定义,因为后处理程序可能已经修改了原始元数据,例如替换值中的占位符... */beanFactory.clearMetadataCache();}
从技术角度看,主要做如下处理:
1、是BeanDefinitionRegistryPostProcessor,调用回调方法-postProcessBeanDefinitionRegistry
2、所有BeanDefinitionPostProcessor,按顺序调用回调方法-postProcessBeanFactory
解析器
解析器较多,以ConfigurationClassPostProcessor举例说明。
ConfigurationClassPostProcessor
类关系图
图中可看出ConfigurationClassPostProcessor是BeanDefinitionRegistryPostProcessor,执行入口为:postProcessBeanDefinitionRegistry
postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
注册BeanDefinition。
//过渡方法@Overridepublic 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);}//处理Configuration类的BeanDefinitionpublic void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {List<BeanDefinitionHolder> configCandidates = new ArrayList<>();String[] candidateNames = registry.getBeanDefinitionNames();// 从所有bean中选择有Configuration注解beanfor (String beanName : candidateNames) {BeanDefinition beanDef = registry.getBeanDefinition(beanName);if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {// bean已经处理过,直接跳过if (logger.isDebugEnabled()) {logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);}}else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {/* checkConfigurationClassCandidate为true是如下三类:1、@Configuration注解的类;2、@Component、@ComponentScan、@Import、@ImportResource注解的类;3、包含@Bean注解方法的类 另:条件为true会设置BeanDefinition 的属性:CONFIGURATION_CLASS_ATTRIBUTE:1、@Configuration类设置为CONFIGURATION_CLASS_FULL2、非@Configuration类设置为CONFIGURATION_CLASS_LITE*/configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));}}// 没有Configuration注解bean,直接返回if (configCandidates.isEmpty()) {return;}/// 按@Order排序(没有@Order均为最低优先级)configCandidates.sort((bd1, bd2) -> {int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());return Integer.compare(i1, i2);});// 对应用容器提供bean名称生成器SingletonBeanRegistry sbr = null;if (registry instanceof SingletonBeanRegistry _sbr) {sbr = _sbr;if (!this.localBeanNameGeneratorSet) {BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);if (generator != null) {this.componentScanBeanNameGenerator = generator;this.importBeanNameGenerator = generator;}}}// 生成环境容器if (this.environment == null) {this.environment = new StandardEnvironment();}/* 解析@Configuration注解的Class */// 生成解析器ConfigurationClassParser ConfigurationClassParser parser = new ConfigurationClassParser(this.metadataReaderFactory, this.problemReporter, this.environment,this.resourceLoader, this.componentScanBeanNameGenerator, registry);// 复制一份候选者用于解析@ConfigurationSet<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);// 已解析的ConfigurationClass容器Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());do {StartupStep processConfig = this.applicationStartup.start("spring.context.config-classes.parse");// 解析处理parser.parse(candidates);// 解析之后验证,一般作用为验证@Configuration注解parser.validate();// 获取已经解析完的configClasses Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());// 删除已经解析的configClasses configClasses.removeAll(alreadyParsed);/* 用ConfigurationClassBeanDefinitionReader注册解析出来的配置类及其Bean类的BeanDefinitionReader到BeanFactory */if (this.reader == null) {this.reader = new ConfigurationClassBeanDefinitionReader(registry, this.sourceExtractor, this.resourceLoader, this.environment,this.importBeanNameGenerator, parser.getImportRegistry());}this.reader.loadBeanDefinitions(configClasses);// 加入到已解析configClasses集合中alreadyParsed.addAll(configClasses);processConfig.tag("classCount", () -> String.valueOf(configClasses.size())).end();// 将已经解析过的configClasses从集合中删除,以方便后续添加其它的配置类对象candidates.clear();/* 判断BeanFactory中的BeanDefinition数量是否大于解析前的BeanDefinition数量。如果大于,则重新获取所有BeanDefinition,然后过滤掉已经解析过的alreadyParsedClasses,再次解析加载一次。原因是前面的Reader对象调用loadBeanDefinition方法时可能会在注册中心中再注册额外的bean定义,且是没有解析过的,因此可能会遗漏一些@Configuration配置类,因此这里需要再获取一次,防止新引入的配置类对象发生遗漏;如此反复,直到没有需解析的ConfigurationClass为止*/if (registry.getBeanDefinitionCount() > candidateNames.length) {// 新的全部BeanDefinitionString[] newCandidateNames = registry.getBeanDefinitionNames();// 老的全部BeanDefinitionSet<String> oldCandidateNames = Set.of(candidateNames);// 已解析的ConfigurationClass容器Set<String> alreadyParsedClasses = new HashSet<>();for (ConfigurationClass configurationClass : alreadyParsed) {alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());}/* 找出需解析的ConfigurationClass,只要candidates不为空,就循环解析直至没有新的需解析的ConfigurationClass为止 */for (String candidateName : newCandidateNames) {if (!oldCandidateNames.contains(candidateName)) {// 不是老的BeanDefinitionBeanDefinition bd = registry.getBeanDefinition(candidateName);if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&!alreadyParsedClasses.contains(bd.getBeanClassName())) {// 必须是注解类 且 不包含在已解析的ConfigurationClass容器candidates.add(new BeanDefinitionHolder(bd, candidateName));}}}// 本次全部的BeanDefinition作为下次循环检查的标准candidateNames = newCandidateNames;}}while (!candidates.isEmpty()); // candidates内容是动态变化的// Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes// 注册ImportRegistry bean,支持ImportAware @Configuration类if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());}// 保存解析器中PropertySourceDescriptors this.propertySourceDescriptors = parser.getPropertySourceDescriptors();if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory cachingMetadataReaderFactory) {// 清除MetadataReaderFactory中的缓存cachingMetadataReaderFactory.clearCache();}}
ConfigurationClassParser
ConfigurationClass 的解析器。
parse(Set configCandidates)
解析注解类。
public void parse(Set<BeanDefinitionHolder> configCandidates) {for (BeanDefinitionHolder holder : configCandidates) {BeanDefinition bd = holder.getBeanDefinition();try {if (bd instanceof AnnotatedBeanDefinition annotatedBeanDef) {// 注解类的解析parse(annotatedBeanDef.getMetadata(), holder.getBeanName());}else if (bd instanceof AbstractBeanDefinition abstractBeanDef && abstractBeanDef.hasBeanClass()) {// 抽象BeanDefinition解析parse(abstractBeanDef.getBeanClass(), holder.getBeanName());}else {// 其它BeanDefinition解析parse(bd.getBeanClassName(), holder.getBeanName());}}catch (BeanDefinitionStoreException ex) {throw ex;}catch (Throwable ex) {throw new BeanDefinitionStoreException("Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);}}this.deferredImportSelectorHandler.process();}// 三个解析方法最终都调用processConfigurationClass,只是构建ConfigurationClass方式不同protected final void parse(@Nullable String className, String beanName) throws IOException {Assert.notNull(className, "No bean class name for configuration class bean definition");MetadataReader reader = this.metadataReaderFactory.getMetadataReader(className);processConfigurationClass(new ConfigurationClass(reader, beanName), DEFAULT_EXCLUSION_FILTER);}protected final void parse(Class<?> clazz, String beanName) throws IOException {processConfigurationClass(new ConfigurationClass(clazz, beanName), DEFAULT_EXCLUSION_FILTER);}protected final void parse(AnnotationMetadata metadata, String beanName) throws IOException {processConfigurationClass(new ConfigurationClass(metadata, beanName), DEFAULT_EXCLUSION_FILTER);}
processConfigurationClass(ConfigurationClass configClass, Predicate filter)
protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) throws IOException {// 应当跳过的类:基于用@Conditional注解引入的判断条件来判断if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {return;}ConfigurationClass existingClass = this.configurationClasses.get(configClass);if (existingClass != null) {if (configClass.isImported()) {if (existingClass.isImported()) {existingClass.mergeImportedBy(configClass);}// 配置类是import类且已经被导入过了,无需再次解析,直接返回return;}else {// 如果不是import类,且存在新的且没有解析过,则使用新的配置类替换老的this.configurationClasses.remove(configClass);this.knownSuperclasses.values().removeIf(configClass::equals);}}/* 循环调用doProcessConfigurationClass方法解析ConfigurationClass及其继承层次结构 */// 获取ConfigurationClass的Class字节码SourceClass sourceClass = asSourceClass(configClass, filter);do {// 从源码解析。因为配置类可能存在层级,因此该循环是针对配置类的各层级类进行的sourceClass = doProcessConfigurationClass(configClass, sourceClass, filter);}while (sourceClass != null);// 将解析好的配置类放入集合中this.configurationClasses.put(configClass, configClass);}
doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass, Predicate filter)
通过从源类中读取注释、成员和方法,应用处理并构建完整的ConfigurationClass。
@Nullableprotected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)throws IOException {// 解析@Component注解配置类 注1if (configClass.getMetadata().isAnnotated(Component.class.getName())) {// @Component配置类成员中可能嵌套有注解配置类,需递归处理processMemberClasses(configClass, sourceClass, filter);}// 解析@PropertySource 注解配置类-该注解的作用是引入额外的properties文件,解析过后再将文件属性值注入到environment中,如果有相同的则替换新的for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(sourceClass.getMetadata(), PropertySources.class,org.springframework.context.annotation.PropertySource.class)) {if (this.propertySourceRegistry != null) {this.propertySourceRegistry.processPropertySource(propertySource);}else {logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +"]. Reason: Environment must implement ConfigurableEnvironment");}}/* 解析@ComponentScan注解配置类 */// 获取@ComponentScan注解对应属性(可能多个)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) {// 用componentScanParse扫描解析,每个componentScan都可能扫描到多个BeanDefinitionSet<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/* 对扫描到的BeanDefinition,判断是否ConfigurationClass,是的话直接进行解析 */for (BeanDefinitionHolder holder : scannedBeanDefinitions) {BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();if (bdCand == null) {bdCand = holder.getBeanDefinition();}if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {// 递归解析扫描到的ConfigurationClassparse(bdCand.getBeanClassName(), holder.getBeanName());}}}}// 解析@Import 注解配置类processImports(configClass, sourceClass, getImports(sourceClass), filter, true);// 解析@ImportResource 注解配置类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);}}// 解析@Bean 方法(对应的是bean)Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);for (MethodMetadata methodMetadata : beanMethods) {configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));}// 在配置类实现的接口上注册默认方法processInterfaces(configClass, sourceClass);//当前类有父类,返回父类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 recursereturn sourceClass.getSuperClass();}}//当前类没有父类,返回nullreturn null;}
注1:@Configuration、@Controller、@Service、@Repository本身就是@Component。@Configuration源代码如下图: