Spring Boot系列之条件注解

概述

想要搞懂Spring Boot自动配置,绕不过条件注解,即@Conditional,可用于根据某个特定的条件来判断是否需要创建某个特定的Bean。本文分析基于spring-boot-autoconfigure-3.2.4版本。

@Conditional注解可以添加在被@Configuration、@Component、@Service等修饰的类,或在被@Bean修饰的方法上,用于控制类或方法对应的Bean是否需要创建。

@Conditional注解需要和Condition接口搭配一起使用。通过对应Condition接口来告知是否满足匹配条件。

扩展注解

条件注解对应Condition处理类解释
ConditionalOnClassOnClassCondition类加载器中存在指定类
ConditionalOnMissingClassOnClassCondition类加载器中不存在指定类
ConditionalOnBeanOnBeanConditionSpring容器中存在指定Bean
ConditionalOnMissingBeanOnBeanConditionSpring容器中不存在指定Bean
ConditionalOnSingleCandidateOnBeanConditionSpring容器中是否存在且只存在一个对应的实例,或虽然有多个但是指定首选的Bean生效
ConditionalOnJavaOnJavaCondition指定Java版本符合要求生效
ConditionalOnJndiOnJndiCondition存在JNDI
ConditionalOnCloudPlatformOnCloudPlatformCondition云平台,支持:CLOUD_FOUNDRY、HEROKU、SAP、NOMAD、KUBERNETES
ConditionalOnCheckpointRestore存在类orc.crac.Resource
ConditionalOnWebApplicationOnWebApplicationConditionWeb应用生效
ConditionalOnNotWebApplicationOnWebApplicationCondition不是Web应用生效
ConditionalOnWarDeploymentOnWarDeploymentConditionWar应用生效
ConditionalOnNotWarDeploymentOnWarDeploymentCondition不是War应用生效
ConditionalOnResourceOnResourceCondition当指定资源文件出现则生效
ConditionalOnPropertyOnPropertyCondition应用环境中的属性满足条件生效
ConditionalOnExpressionOnExpressionCondition判断SpEL表达式成立生效
ConditionalOnThreadingOnThreadingCondition指定线程处于active状态

ConditionalOnCheckpointRestore源码如下:

@ConditionalOnClass(name = {"org.crac.Resource"})
public @interface ConditionalOnCheckpointRestore {
}

CRaC是OpenJDK项目,有兴趣可延伸阅读。

原理

条件注解存在的意义在于动态识别,即代码自动化执行。如@ConditionalOnClass会检查类加载器中是否存在对应的类,如果有的话被注解修饰的类就有资格被Spring容器所注册,否则会被skip。

如FreemarkerAutoConfiguration这个自动化配置类的定义如下:

@AutoConfiguration
@ConditionalOnClass({ freemarker.template.Configuration.class, FreeMarkerConfigurationFactory.class })
@EnableConfigurationProperties(FreeMarkerProperties.class)
@Import({ FreeMarkerServletWebConfiguration.class, FreeMarkerReactiveWebConfiguration.class, FreeMarkerNonWebConfiguration.class })
public class FreeMarkerAutoConfiguration {
}

这个自动化配置类被@ConditionalOnClass条件注解修饰,判断类加载器中是否存在freemarker.template.Configuration和FreeMarkerConfigurationFactory这两个类,如果都存在的话会在Spring容器中加载这个FreeMarkerAutoConfiguration配置类;否则不会加载。

@Conditional源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
	Class<? extends Condition>[] value();
}

需要传入一个Class数组,数组类型是Condition。而Condition是个接口,用于匹配组件是否有资格被容器注册:

@FunctionalInterface
public interface Condition {
	// ConditionContext内部会存储Spring容器、应用程序环境信息、资源加载器、类加载器
	boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}

@Conditional注解属性中可以持有多个Condition接口的实现类,所有的Condition接口需要全部匹配成功后这个@Conditional修饰的组件才有资格被注册。

Condition有个子接口ConfigurationCondition:

public interface ConfigurationCondition extends Condition {
	ConfigurationPhase getConfigurationPhase();
    public static enum ConfigurationPhase {
        PARSE_CONFIGURATION,
        REGISTER_BEAN
    }
}

这个子接口是一种特殊的条件接口,多一个getConfigurationPhase方法,也就是条件注解的生效阶段。只有在ConfigurationPhase中定义的两种阶段下才会生效:

  • PARSE_CONFIGURATION
  • REGISTER_BEAN

Condition接口有个抽象类SpringBootCondition,SpringBoot中所有条件注解对应的条件类都继承这个抽象类,并需要实现matches方法:

@Override
public final boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String classOrMethodName = getClassOrMethodName(metadata); // 得到类名或者方法名(条件注解可以作用的类或者方法上)
    try {
        ConditionOutcome outcome = getMatchOutcome(context, metadata); // 抽象方法,具体子类实现。ConditionOutcome记录了匹配结果boolean和log信息
        logOutcome(classOrMethodName, outcome); // log记录一下匹配信息
        recordEvaluation(context, classOrMethodName, outcome); // 报告记录一下匹配信息
        return outcome.isMatch(); // 返回是否匹配
    } catch (NoClassDefFoundError ex) {
        throw new IllegalStateException("Could not evaluate condition on " + classOrMethodName + " due to " + ex.getMessage() + " not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)", ex);
    } catch (RuntimeException ex) {
        throw new IllegalStateException("Error processing condition on " + getName(metadata), ex);
    }
}

基于Class的条件注解

有两个

  • @ConditionalOnClass
  • @ConditionalOnMissingClass

@ConditionalOnClass注解定义如下:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
	Class<?>[] value() default {}; // 需要匹配的类
	String[] name() default {}; // 需要匹配的类名
}

它有2个属性,分别是类数组和字符串数组(作用一样,类型不一样),而且被@Conditional注解所修饰。

对应条件类是OnClassCondition:

@Order(Ordered.HIGHEST_PRECEDENCE) // 优先级最高级别
class OnClassCondition extends FilteringSpringBootCondition {
	@Override
	protected final ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses,
			AutoConfigurationMetadata autoConfigurationMetadata) {
		// Split the work and perform half in a background thread if more than one
		// processor is available. Using a single additional thread seems to offer the
		// best performance. More threads make things worse.
		if (autoConfigurationClasses.length > 1 && Runtime.getRuntime().availableProcessors() > 1) {
			return resolveOutcomesThreaded(autoConfigurationClasses, autoConfigurationMetadata);
		}
		else {
			OutcomesResolver outcomesResolver = new StandardOutcomesResolver(autoConfigurationClasses, 0,
					autoConfigurationClasses.length, autoConfigurationMetadata, getBeanClassLoader());
			return outcomesResolver.resolveOutcomes();
		}
	}
}

比如FreemarkerAutoConfiguration中的@ConditionalOnClass注解中有value属性是freemarker.template.Configuration.classFreeMarkerConfigurationFactory.class。在OnClassCondition执行过程中得到的最终ConditionalOutcome中的log message如下:
@ConditionalOnClass classes found: freemarker.template.Configuration,org.springframework.ui.freemarker.FreeMarkerConfigurationFactory

基于Bean的条件注解

有3个:

  • @ConditionalOnBean
  • @ConditionalOnMissingBean
  • @ConditionalOnSingleCandidate

和基于类的条件注解比较类似。

激活机制

这部分有点难,想通过阅读源码来理清楚前后调用及解析关系。好在我们可以断点调试。通过断点调试发现关键类和方法:

  • ConfigurationClassParser
  • ConditionEvaluator
  • ComponentScanAnnotationParser

SpringBoot使用ConditionEvaluator这个内部类完成条件注解的解析和判断。在Spring容器的refresh过程中,只有跟解析或者注册bean有关系的类都会使用ConditionEvaluator完成条件注解的判断,这个过程中一些类不满足条件的话就会被skip。这些类比如有AnnotatedBeanDefinitionReader、ConfigurationClassBeanDefinitionReader、ConfigurationClassParse、ClassPathScanningCandidateComponentProvider等。

比如ConfigurationClassParser的构造函数会初始化内部属性conditionEvaluator:

public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory,
    ProblemReporter problemReporter, Environment environment, ResourceLoader resourceLoader,
    BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry) {
	this.metadataReaderFactory = metadataReaderFactory;
	this.problemReporter = problemReporter;
	this.environment = environment;
	this.resourceLoader = resourceLoader;
	this.registry = registry;
	this.componentScanParser = new ComponentScanAnnotationParser(resourceLoader, environment, componentScanBeanNameGenerator, registry);
	// 构造ConditionEvaluator用于处理条件注解
	this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader);
}

ConfigurationClassParser对每个配置类进行解析的时候都会使用ConditionEvaluator:

if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
	return;
}

ConditionEvaluator的skip方法:

public boolean shouldSkip(AnnotatedTypeMetadata metadata, ConfigurationPhase phase) {
	// 如果这个类没有被@Conditional注解所修饰,不会skip
    if (metadata == null || !metadata.isAnnotated(Conditional.class.getName())) {
        return false;
    }
    // 如果参数中沒有设置条件注解的生效阶段
    if (phase == null) {
        // 是配置类的话直接使用PARSE_CONFIGURATION阶段
        if (metadata instanceof AnnotationMetadata && ConfigurationClassUtils.isConfigurationCandidate((AnnotationMetadata) metadata)) {
            return shouldSkip(metadata, ConfigurationPhase.PARSE_CONFIGURATION);
        }
        // 否则使用REGISTER_BEAN阶段
        return shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN);
    }
    // 要解析的配置类的条件集合
    List<Condition> conditions = new ArrayList<Condition>();
    // 获取配置类的条件注解得到条件数据,并添加到集合中
    for (String[] conditionClasses : getConditionClasses(metadata)) {
        for (String conditionClass : conditionClasses) {
            Condition condition = getCondition(conditionClass, this.context.getClassLoader());
            conditions.add(condition);
        }
    }
    // 对条件集合做个排序
    AnnotationAwareOrderComparator.sort(conditions);
    // 遍历条件集合
    for (Condition condition : conditions) {
        ConfigurationPhase requiredPhase = null;
        if (condition instanceof ConfigurationCondition) {
            requiredPhase = ((ConfigurationCondition) condition).getConfigurationPhase();
        }
        // 没有这个解析类不需要阶段的判断或者解析类和参数中的阶段一致才会继续进行
        if (requiredPhase == null || requiredPhase == phase) {
            // 阶段一致切不满足条件的话,返回true并跳过这个bean的解析
            if (!condition.matches(this.context, metadata)) {
                return true;
            }
        }
    }
    return false;
}

SpringBoot在条件注解的解析log记录在ConditionEvaluationReport类中,可通过BeanFactory获取。BeanFactory是有父子关系的;每个BeanFactory都存有一份ConditionEvaluationReport,互不相干:

ConditionEvaluationReport conditionEvaluationReport = beanFactory.getBean("autoConfigurationReport", ConditionEvaluationReport.class);
Map<String, ConditionEvaluationReport.ConditionAndOutcomes> result = conditionEvaluationReport.getConditionAndOutcomesBySource();
for(String key : result.keySet()) {
    ConditionEvaluationReport.ConditionAndOutcomes conditionAndOutcomes = result.get(key);
    Iterator<ConditionEvaluationReport.ConditionAndOutcome> iterator = conditionAndOutcomes.iterator();
    while(iterator.hasNext()) {
        ConditionEvaluationReport.ConditionAndOutcome conditionAndOutcome = iterator.next();
        System.out.println(key + " -- " + conditionAndOutcome.getCondition().getClass().getSimpleName() + " -- " + conditionAndOutcome.getOutcome());
    }
}

打印出条件注解下的类加载信息:

...
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: freemarker.template.Configuration,org.springframework.ui.freemarker.FreeMarkerConfigurationFactory
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: groovy.text.markup.MarkupTemplateEngine
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: com.google.gson.Gson
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: org.h2.server.web.WebServlet
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: org.springframework.hateoas.Resource,org.springframework.plugin.core.Plugin
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: com.hazelcast.core.HazelcastInstance
...

实战

自定义

需要自定义一个condition类实现Condition接口,假设根据系统类型来加载不同的Bean:

public class OnSystemCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ConditionalOnSystem.class.getName());
        if (annotationAttributes == null) {
            return false;
        }
        ConditionalOnSystem.SystemType systemType = (ConditionalOnSystem.SystemType) annotationAttributes.get("type");
        switch (systemType) {
            case WINDOWS:
                return context.getEnvironment().getProperty("os.name").contains("Windows");
            case LINUX:
                return context.getEnvironment().getProperty("os.name").contains("Linux ");
            case MAC:
                return context.getEnvironment().getProperty("os.name").contains("Mac ");
        }
        return false;
    }
}

自定义条件注解并指定对应的处理condition类:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(OnSystemCondition.class)
public @interface ConditionalOnSystem {
    /**
     * 指定系统
     */
    SystemType type() default SystemType.WINDOWS;
    /**
     * 系统类型
     */
    enum SystemType {
        WINDOWS,
        LINUX,
        MAC;
    }
}

参考

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

引入高德地图

1、配置 试试keytool 有没有反应 就算java -version没问题也一定是你没配path路径 在系统中配到bin就行了 2、获取密钥 网上真的坑太多了还有有chat问了一下 keytool -v -list -keystore "C:\Users\xxxx\.android\debug.keystore"执行这个你看你的 3、去高德地…

QFileDialog窗口没有文件选择路径框问题的处理方法

QFileDialog作为QT自带的文件对话框&#xff0c;其界面有挑选文件路径的区域 但在某些操作系统下&#xff08;如欧拉操作系统&#xff09;&#xff0c;文件挑选框QFileDialogLineEdit可能会隐藏&#xff0c;导致无法选择文件路径 解决方法&#xff1a; QFileDialog* fd; fd-&…

【团体程序设计天梯赛】往年关键真题 L2-026 小字辈 递归 L2-027 名人堂与代金券 排序 详细分析完整AC代码

【团体程序设计天梯赛 往年关键真题 详细分析&完整AC代码】搞懂了赛场上拿下就稳 【团体程序设计天梯赛 往年关键真题 25分题合集 详细分析&完整AC代码】&#xff08;L2-001 - L2-024&#xff09;搞懂了赛场上拿下就稳了 【团体程序设计天梯赛 往年关键真题 25分题合…

2024最新docker部署gitlab

docker部署gitlab 快速命令 1 拉取镜像 docker pull gitlab/gitlab-ce2 启动容器 docker run -itd \-p 9980:80 \-p 9922:22 \-v /opt/soft/docker/gitlab/etc:/etc/gitlab \-v /opt/soft/docker/gitlab/log:/var/log/gitlab \-v /opt/soft/docker/gitlab/opt:/var/opt/g…

Xinlinx FPGA如何降低Block RAM的功耗

FPGA中降低Block RAM的功耗有两种方式&#xff0c;分别是选择合适的写操作模式以及Block RAM的实现算法及综合设置。我们知道对于采用IP核生成对应的RAM时&#xff0c;会有最小面积算法、低功耗算法以及固定原语&#xff0c;但是采用最小功耗算法有时由于级联长度导致无法实现&…

1 集成学习基础

目录 0 简述 1 集成学习算法代表 1.1 Bagging 1.1.1 模型预测的结果组合的方式 1.2 stacking 1.3 blending和stacking优缺点对比 0 简述 集成学习&#xff0c;典型的群殴策略&#xff0c;但是如何组织让彼此配合得当发挥最大的价值是一个值得思考的问题。 集成学习是一…

MySQL-笔记-08.数据库编程

目录 8.1 编程基础 8.1.1 基本语法 8.1.2 运算符与表达式 1. 标识符 2. 常量 &#xff08;1&#xff09; 字符串常量 &#xff08;2&#xff09;日期时间常量 &#xff08;3&#xff09;数值常量 &#xff08;4&#xff09;布尔值常量 &#xff08;5&#xff09;NULL…

2024长三角快递物流展:科技激荡,行业焕发新活力

7月8日&#xff0c;杭州将迎来快递物流科技盛宴&#xff0c;这是一年一度的行业盛会&#xff0c;吸引了全球领先的快递物流企业和创新技术汇聚一堂。届时&#xff0c;会展中心将全方位展示快递物流及供应链、分拣系统、输送设备、智能搬运、智能仓储、自动识别、无人车、AGV机器…

判断前端入参是否空否则提示前端写法

vue2中 前端先声明一个变量&#xff0c;用于alert判断 在templeat中定义一个提示语句 然后在点击事件时判断一下是否展示

API接口知识小结

应用程序接口API&#xff08;Application Programming Interface&#xff09;&#xff0c;是提供特定业务输出能力、连接不同系统的一种约定。这里包括外部系统与提供服务的系统&#xff08;中后台系统&#xff09;或后台不同系统之间的交互点。包括外部接口、内部接口&#xf…

串联超前及对应matlab实现

串联超前校正它的本质是利用相角超前的特性提高系统的相角裕度。传递函数为&#xff1a;下面将以一个实际的例子&#xff0c;使用matlab脚本&#xff0c;实现其校正后的相位裕度≥60。

在VSCode中调试其他软件执行的python文件

在VSCode中调试其他软件执行的python文件 0. 实际场景 我有一段python代码想在Metashape中运行&#xff0c;但是又想在中间某一步停下来查看变量值。由于Metashape的python环境不容易在vscode中配置&#xff0c;所以直接用vscode调试单个文件的方式无法实现这个想法。还好&am…

Java 四大名著之一,豆瓣9.7,Java神作重磅上市

❤️作者主页&#xff1a;小虚竹 ❤️作者简介&#xff1a;大家好,我是小虚竹。2022年度博客之星评选TOP 10&#x1f3c6;&#xff0c;Java领域优质创作者&#x1f3c6;&#xff0c;CSDN博客专家&#x1f3c6;&#xff0c;华为云享专家&#x1f3c6;&#xff0c;掘金年度人气作…

PG修改端口号与error: could not connect to server: could not connect to server 问题解决

刚开始学习PG修改端口号之后数据库端口号没变。 修改端口号&#xff1a;/usr/local/pgsql/data中的postgresql.conf中 修改后并不能直接生效需要重启PG&#xff1a; /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/data/logfile restart重启后新…

R语言--图形绘制

一&#xff0c;绘制简单图形 c1<- c(10,20,30,40,50) c2<-c(2,7,15,40,50) plot(c1,c2,typeb) 具体参数请参考R语言中的绘图技巧1&#xff1a;plot()函数参数汇总_r语言plot参数设置-CSDN博客 c1<- c(10,20,30,40,50) c2<-c(2,7,15,40,50) plot(c1,c2,typeb,col#…

【上岗认证】错题整理记录

目录 &#x1f31e;一、阶段1&#xff1a;编码规范 &#x1f30a;编码规范考试-CC &#x1f31e;二、阶段2&#xff1a;开发基础 &#x1f30a;C/C &#x1f30a;数据库&#xff08;Oracle/MySql&#xff09; &#x1f31e;三、阶段3&#xff1a;测试基础 &#x1f30a;…

智慧校园研究新发展

随着新兴信息技术出现&#xff0c;智慧校园研究发展出新的样态。智慧校园研究新进展主要体现在信息化背景下的未来教育理论发展&#xff0c;以新一代人工智能技术为代表的新兴技术应用&#xff0c;以及“人—技”协作的个性化学习创新。 智慧校园是未来教育的重要入口&#xff…

WPS的JS宏如何设置Word文档的表格的单元格文字重新编号

希望对Word文档中的表格进行统一处理&#xff0c;表格内的编号&#xff0c;有时候会出现紊乱&#xff0c;下一个表格的编号承接了上一个表格的编号&#xff0c;实际需要重新编号。 当表格比较多时&#xff0c;手动更改非常麻烦&#xff0c;而且更改一遍并不能完成&#xff0c;…

vue2—— mixin 超级详细!!!

mixin Mixin是面向对象程序设计语言中的类&#xff0c;提供了方法的实现。其他类可以访问mixin类的方法而不必成为其子类 Mixin类通常作为功能模块使用&#xff0c;在需要该功能时“混入”&#xff0c;有利于代码复用又避免了多继承的复杂 vue中的mixin 先来看一下官方定义 mi…

echarts特殊处理(滚动条、legend分页、tooltip滚动)

当图表数据量过大时&#xff0c;为了使用者能够有更好的体验&#xff0c;对于大数据量的图表处理&#xff1a; 1、当x轴数据过多不能完全展示时&#xff0c;需要添加滚动条&#xff1a;option设置dataZoom字段 dataZoom: [{ // 这部分是关键&#xff0c;设置滚动条type: slide…