Spring boot @ conditional annotation usage example introduction
•
Java
Introduce the @ conditional annotation: spring 5 Version 0.15 @ conditional annotation official document
@Conditional means that a component is eligible for registration only if all specified conditions match.
The @ conditional annotation can be used in any of the following ways:
One of the main source codes of annotation is changed. It is loaded into the spring container only after it meets the conditions through match matching
@Override
public boolean matches(ConditionContext context,AnnotatedTypeMetadata Metadata) {
if (context.getEnvironment() != null) {
// Read the @Profile annotation attributes
MultiValueMap<String,Object> attrs = Metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
}
return true;
}@H_301_19@
作用:总而言之,只有@Conditional指定的条件成立,才给容器添加组件
@Conditional派生注解:@Conditional派生了很多注解,下面给个表格列举一下派生注解的用法
301
| @Conditional派生注解 | 作用(都是判断是否符合指定的条件) |
|---|---|
| @ConditionalOnJava | 系统的java版本是否符合要求 |
| @ConditionalOnBean | 有指定的Bean类 |
| @ConditionalOnMissingBean | 没有指定的bean类 |
| @ConditionalOnExpression | 符合指定的SpEL表达式 |
| @ConditionalOnClass | 有指定的类 |
| @ConditionalOnMissingClass | 没有指定的类 |
| @ConditionalOnSingleCandidate | 容器只有一个指定的bean,或者这个bean是首选bean |
| @ConditionalOnProperty | 指定的property属性有指定的值 |
| @ConditionalOnResource | 路径下存在指定的资源 |
| @ConditionalOnWebApplication | 系统环境是web环境 |
| @ConditionalOnNotWebApplication | 系统环境不是web环境 |
| @ConditionalOnjndi | JNDI存在指定的项 |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。
总结
以上是编程之家为你收集整理的Spring Boot @Conditional注解用法示例介绍全部内容,希望文章能够帮你解决Spring Boot @Conditional注解用法示例介绍所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
