Spring IOC - ConfigurationClassPostProcessor源码解析

上文提到Spring在Bean扫描过程中,会手动将5个Processor类注册到beanDefinitionMap中,其中ConfigurationClassPostProcessor就是本文将要讲解的内容,该类会在refresh()方法中通过调用invokeBeanFactoryPosstProcessors(beanFactory)被调用。
5个Processor类列表如下:
类名
是否BeanDefinitionRegistryPostProcessor
是否BeanFactoryPostProcessor
是否BeanPostProcessor
ConfigurationClassPostProcessor
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
EventListenerMethodProcessor
DefaultEventListenerFactory
ConfigurationClassPostProcessor中两个核心方法将被调用,其主要作用如下:

方法名

作用

postProcessBeanDefinitionRegistry()

(1)@Conditional注解条件解析

(2)被以下注解标注的类及内部类

@Component、@PropertySources、@PropertySource、@ComponentScans、@ComponentScan、@Import、@ImportResource、@Bean、接口中的@Bean

postProcessBeanFactory()

对full模式的配置类BeanDefinition进行CGLib增强:把CGLib生成的子类型设置到beanDefinition中

postProcessBeanDefinitionRegistry()源码及注释如下:
@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);}
进入processConfigBeanDefinitions(registry)方法,其主体逻辑流程图如下:
其中标红步骤为两个关键步骤:

        1. 通过beanDefinition判断是否为配置类

        Spring专门提供了一个工具类:ConfigurationClassUtils来检查是否为配置类,并标记full、lite模式,逻辑如下:

        2. 解析配置类
doProcessConfigurationClass即为真正的对个注解的解析方法,其逻辑流程图如下:

        具体的源码及详细注释如下:

        1. 主流程processConfigBeanDefinitions方法:

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {// 候选配置类定义列表List<BeanDefinitionHolder> configCandidates = new ArrayList<>();// 获取容器中所有bean定义的名字String[] candidateNames = registry.getBeanDefinitionNames();for (String beanName : candidateNames) {// 获取bean定义BeanDefinition beanDef = registry.getBeanDefinition(beanName);// org.springframework.context.annotation.ConfigurationClassPostProcessor.configurationClass// Bean Definition是否已经被标记为配置类(full、lite模式)if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {if (logger.isDebugEnabled()) {logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);}}// 查看bean是否是ConfigurationClass//被@Configuration、@Component、@ComponentScan、@Import、@ImportResource、@Bean标记else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {// 是配置类,则加入到候选列表configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));}}// Return immediately if no @Configuration classes were found// 如果为空,即找不到被@Configuration、@Component、@ComponentScan、@Import、@ImportResource、@Bean标记的类,则立即返回if (configCandidates.isEmpty()) {return;}// Sort by previously determined @Order value, if applicable// 对需要处理的BeanDefinition排序,值越小越靠前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// 检查是否有自定义的beanName生成器SingletonBeanRegistry sbr = null;if (registry instanceof SingletonBeanRegistry) {sbr = (SingletonBeanRegistry) registry;if (!this.localBeanNameGeneratorSet) {//获取自定义的beanName生成器BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);if (generator != null) {// 如果spring有自定义的beanName生成器,则重新赋值this.componentScanBeanNameGenerator = generator;this.importBeanNameGenerator = generator;}}}//如果环境对象为空,则创建新的环境对象if (this.environment == null) {this.environment = new StandardEnvironment();}// 构造一个配置类解析器,用来把bean定义的重要信息提取转化为ConfigurationClass// Parse each @Configuration classConfigurationClassParser parser = new ConfigurationClassParser(this.metadataReaderFactory, this.problemReporter, this.environment,this.resourceLoader, this.componentScanBeanNameGenerator, registry);// 候选配置类定义列表(查重)Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);// 存储已解析的配置类列表Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());do {// 解析配置类parser.parse(candidates);// 配置类不能被申明为final(因为CGLIB的限制),除非它声明proxyBeanMethods=false// 在@Configuration类中,@Bean方法的实例必须是可重写的,以适应CGLIBparser.validate();// 获取解析器中解析出的配置类ConfigurationClass: parser.getConfigurationClasses()Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());// 过滤掉已解析的配置类configClasses.removeAll(alreadyParsed);// 构造一个bean定义读取器// Read the model and create bean definitions based on its contentif (this.reader == null) {this.reader = new ConfigurationClassBeanDefinitionReader(registry, this.sourceExtractor, this.resourceLoader, this.environment,this.importBeanNameGenerator, parser.getImportRegistry());}// 读取ConfigurationClass获取衍生bean定义并注册到容器// 核心方法,将完全填充好的ConfigurationClass实例转化为BeanDefinition注册入IOC容器this.reader.loadBeanDefinitions(configClasses);// 加入已解析配置类alreadyParsed.addAll(configClasses);// 清空候选配置类定义列表candidates.clear();// 如果容器中bean定义有新增if (registry.getBeanDefinitionCount() > candidateNames.length) {// 查找出新增的配置类bean定义 startString[] 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// 如果想要在@Configuration类中使用ImportAware接口,需要将ImportRegistry注册为bean。这样,@Configuration类就可以// 使用ImportRegistry来获取导入的类信息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.// 如果MetadataReaderFactory是由外部提供的,则需要清楚其缓存。但是,如果MetadataReaderFactory是由ApplicationContext共享的,则不需要进行清除,// 因为ApplicationContext会自动清楚共享缓存((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();}}

        2. 其中检查是否为配置类方法ConfigurationClassUtils.checkConfigurationClassCandidate

//检查给定的BeanDefinition是否是一个配置类的候选者(或一个在配置/组件类中声明的嵌套组件类)//并对其进行相应的标记处理//被@Configuration、@Component、@ComponentScan、@Import、@ImportResource、@Bean标记public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) {//获取bean定义信息中的class类名String className = beanDef.getBeanClassName();//如果className为空,或者bean定义信息中的factoryMethod不等于空,那么直接返回if (className == null || beanDef.getFactoryMethodName() != null) {return false;}AnnotationMetadata metadata;//通过注解注入的BeanDefinition都是AnnotatedGenericBeanDefinition,实现了AnnotatedBeanDefinition//Spring内部的BeanDefinition都是RootBeanDefiniton,实现了AbstractBeanDefinition//此处主要用于判断是否是归属于AnnotatedBeanDefinitionif (beanDef instanceof AnnotatedBeanDefinition &&className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) {// Can reuse the pre-parsed metadata from the given BeanDefinition...// 从当前bean的定义信息中获取元素metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata();}//判断是否是Spring中默认的BeanDefinitionelse 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.//获取当前bean对象的Class对象Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass();//如果class实例是下面4种类或者接口的子类,父接口等任何一种情况,直接返回if (BeanFactoryPostProcessor.class.isAssignableFrom(beanClass) ||BeanPostProcessor.class.isAssignableFrom(beanClass) ||AopInfrastructureBean.class.isAssignableFrom(beanClass) ||EventListenerFactory.class.isAssignableFrom(beanClass)) {return false;}//为给定类创建新的AnnotationMetadatametadata = AnnotationMetadata.introspect(beanClass);}//如果上述两种情况都不符合else {try {//获取className的MetadataReader实例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;}}// 获取Bean Definition的元数据被@Configuration注解标注的属性字典值Map<String, Object> config = metadata.getAnnotationAttributes(Configuration.class.getName());// 如果bean被@Configuration注解标注,并且proxyBeanMethods属性的值为true,则标注当前配置类为full,即需要代理增强,为一个代理类if (config != null && !Boolean.FALSE.equals(config.get("proxyBeanMethods"))) {beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);}// 如果存在,或者isConfigurationCandidate返回true,则标注当前配置类为lite,即不需要代理增强,为一个普通类// 标注了Configuration注解且proxyBeanMethods属性的值为false,则为lite模式// 没有标注Configuration注解,但是标注了Component、ComponentScan、Import、ImportResource、@Bean注解、则为lite模式else if (config != null || isConfigurationCandidate(metadata)) {beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);}// 组件类不存在@Configuration注解,直接返回falseelse {return false;}// 获取配置类的排序顺序,设置到组件定义属性中// It's a full or lite configuration candidate... Let's determine the order value, if any.//Bean Definition是一个标记为full/lite的候选项,如果有order属性就设置order属性Integer order = getOrder(metadata);if (order != null) {//设置bean的order值beanDef.setAttribute(ORDER_ATTRIBUTE, order);}return true;}

        3. 真正干活的方法doProessConfigurationClass

// 通过读取源类的注释、成员和方法来构建完整的ConfigurationClass// 1.处理@Component// 2.处理每个@PropertySource// 3.处理每个@ComponentScan// 4.处理每个Impoit// 5.处理每个@ImportResource// 6.找配置类中@Bean注解的方法(找出Class中存在@Bean标注的方法,加入到ConfigurationClass的beanMethods属性)// 7.找接口中@Bean注解的方法// 8.存在父类则递归处理@Nullableprotected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)throws IOException {if (configClass.getMetadata().isAnnotated(Component.class.getName())) {// Recursively process any member (nested) classes first// 递归处理嵌套的成员类processMemberClasses(configClass, sourceClass, filter);}// Process any @PropertySource annotations// 处理@PropertySources和@PropertySource注解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// 处理@ComponentScans和@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) {// The config class is annotated with @ComponentScan -> perform the scan immediately// 配置类使用了注解@ComponentScan -> 立即执行扫描。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 neededfor (BeanDefinitionHolder holder : scannedBeanDefinitions) {BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();if (bdCand == null) {bdCand = holder.getBeanDefinition();}if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {parse(bdCand.getBeanClassName(), holder.getBeanName());}}}}// Process any @Import annotations// 处理@Import注解// getImports:递归获取@Import导入的类processImports(configClass, sourceClass, getImports(sourceClass), filter, true);// Process any @ImportResource annotations// 处理@ImportResource注解// 读取locations, reader属性,封装存放在importResources map集合中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// 处理接口的默认方法实现,从jdk8开始,接口中的方法可以有自己的默认实现,因此如果这个接口的方法加了@Bean注解,也需要被解析processInterfaces(configClass, sourceClass);// Process superclass, if anyif (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();}}// No superclass -> processing is completereturn null;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/126215.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php收发邮件的多种方法?

1、添加扩展&#xff1a; # 第一种&#xff1a; composer require php-imap/php-imap # 第二种&#xff1a; composer require phpmailer/phpmailer2、这里采用第二种方式&#xff1a; <?php declare(strict_types1);namespace App\Controller\v1\email;use App\Controll…

蓝牙服务:优化体验,提高连接效率

文章目录 1. 对蓝牙连接进行优化2. 设备配对的缓存机制3. 优化蓝牙连接的稳定性 蓝牙技术已经成为我们生活中不可或缺的一部分&#xff0c;我们使用它进行音频传输、数据传输、设备连接等等。然而&#xff0c;有时蓝牙连接会让用户感到非常困扰&#xff0c;比如连接速度缓慢、连…

【Android知识笔记】换肤专题

换肤其实也属于插件化专题的一个子话题,之所以单独拿出来,是因为它的处理方式比较特殊,相比插件化而言较简单一些。 系统内置的换肤功能支持 - Theme Android 系统中如果想修改应用的背景色,最简单的就是利用以下Theme相关的属性: 使用这些内置的属性可以实现一定程度上…

关于CMMI3.0评估的相关事宜

CMMI研究院发布了关于CMMI3.0评估的相关事宜&#xff0c;具体安排如下&#xff1a; 1、于2023年4月6日正式发布 CMMI 3.0。 2、于2023年第三季度&#xff08;7至9月&#xff09;计划确定具体方式和时间表&#xff0c;以获取CMMI3.0资质&#xff08;具体安排根据委员会确定&am…

计算机视觉的相机选型

#你一般什么时候会用到GPT?# 目前市面上的工业相机大多是基于CCD&#xff08;ChargeCoupled Device&#xff09;或CMOS&#xff08;Complementary Metal Oxide Semiconductor&#xff09;芯片的相机。一般CCD制造工艺更加复杂&#xff0c;也会更贵一点&#xff01; 1、CCD工…

Vue.js 之 透传 Attributes

Vue.js 之 透传 Attributes 1.Attributes 继承​2.对 class 和 style 的合并​3.v-on 监听器继承​4.深层组件继承​5.禁用 Attributes 继承​6.多根节点的 Attributes 继承​7.在 JavaScript 中访问透传 Attributes​ 1.Attributes 继承​ “透传 attribute”指的是传递给一个…

catkin build说明

与catkin_make不同&#xff0c;可以参考catkin build – Build Packages中文翻译版手册catkin build 默认会生成.private文件夹。链接时候可能出现失败的问题。catkin参数配置之软连接模式 改为如下模式即可 catkin init catkin config --merge-devel catkin buildBug Issue1…

django如何连接sqlite数据库?

目录 一、SQLite数据库简介 二、Django连接SQLite数据库 1、配置数据库 2、创建数据库表 三、使用Django ORM操作SQLite数据库 1、定义模型 2、创建对象 3、查询对象 总结 本文将深入探讨如何在Django框架中连接和使用SQLite数据库。我们将介绍SQLite数据库的特点&…

springboot知识点补充

系列文章目录 文章目录 系列文章目录前言一、springboot基础二、使用步骤1.引入库2.读入数据 前言 一、springboot基础 知识点补充 二、使用步骤 1.引入库 代码如下&#xff08;示例&#xff09;&#xff1a; import numpy as np import pandas as pd import matplotlib.pyp…

前端开发规范和注意点

*补救原则&#xff1a;出现异常时第一时间退出&#xff0c;并给出足够的提示错误信息。 *开发流程&#xff1a; *常规发布版本 常规发布版本频率为每月会有两个发布窗口&#xff08;一大一小&#xff1a;上半月小版本、下半月大版本&#xff09;。 例如2019年4月的常规发布版本…

k8spod

pod基本概念 (几种容器) pod 是k8s最小的创建和运行单元 一个pod包含几个容器&#xff0c;1个根容器/父容器/基础容器&#xff0c;一个或者多个应用容器/业务容器&#xff0c;pause容器 pod里面容器共享 network UTS IPC命令空间 k8s 创建的Pod 分为两种&#xff1a; 自主…

Android NDK开发详解之ndk-gdb

Android NDK开发详解之ndk-gdb 要求用法选项 线程支持 NDK 包含一个名为 ndk-gdb 的 Shell 脚本&#xff0c;可以启动命令行原生调试会话。偏好使用 GUI 的用户则应阅读在 Android Studio 中调试这篇文档。 要求 要运行命令行原生调试&#xff0c;必须满足以下要求&#xff1…

C#中LINQtoSQL的设置与连接

目录 一、首次安装LinqToSql类 二、非首次安装LinqToSql类 1.接受原有数据库连接 2.建立新的数据库连接 3.建立本地数据库连接 LINQ&#xff08;Language-Integrated Query&#xff0c;语言集成查询&#xff09;是微软公司提供的一项新技术&#xff0c;它能够将查询功能直…

linux常见问题解析

Linux 是一款商业上最受欢迎的操作系统之一&#xff0c;由于其稳定性和安全性&#xff0c;它已成为服务器运维中的主流。虽然 Linux 具有很多优点&#xff0c;但仍然有一些常见问题和挑战需要解决。 本文将梳理一些常见的 Linux 运维问题&#xff0c;并提供如何解决这些问题的…

2023年【熔化焊接与热切割】考试题及熔化焊接与热切割模拟考试

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2023年【熔化焊接与热切割】考试题及熔化焊接与热切割模拟考试&#xff0c;包含熔化焊接与热切割考试题答案和解析及熔化焊接与热切割模拟考试练习。安全生产模拟考试一点通结合国家熔化焊接与热切割考试最新大纲及熔…

亲测有效!盘点3款好用的录屏软件

随着社会的发展&#xff0c;视频内容的制作和共享变得比以往任何时候都更重要。无论是录制在线课程、游戏过程&#xff0c;还是制作教程或视频演示&#xff0c;一款好用的录屏软件都能让用户事半功倍。接下来&#xff0c;我们将介绍三款各自适用于不同场景的录屏软件&#xff0…

C# Onnx Ultra-Fast-Lane-Detection-v2 车道线检测

效果 项目 代码 using Microsoft.ML.OnnxRuntime; using Microsoft.ML.OnnxRuntime.Tensors; using OpenCvSharp; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; usi…

科东软件受邀参加2023国家工业软件大会,共话工业软件未来

10月28日&#xff0c;由中国自动化学会主办的2023国家工业软件大会在浙江湖州开幕。大会以“工业软件智造未来”为主题&#xff0c;一批两院院士、千余名专家学者齐聚一堂&#xff0c;共同探讨工业软件领域前沿理论和技术创新应用问题&#xff0c;共同谋划我国工业软件未来发展…

java初始化list的几种方式

在Java中初始化List有以下几种常见的方式: 使用Arrays.asList()静态方法: List<Integer> list1 Arrays.asList(1, 2, 3);使用List接口的实现类ArrayList的构造函数: List<String> list2 new ArrayList<>();使用Collections.singletonList() String obj…

实用篇-Linux

一、Linux介绍 linux特点 免费开源多用户多任务 Linux系统版本分为内核版和发行版 发行版是基于内核版进行扩展&#xff0c;由各个Linux厂商开发和维护&#xff0c;因为我们真正使用linux最终安装的其实是linux的发行版 下面以CentOS为例来学习Linux 二、Linux安装 安装方式…