springboot注解说明(@ConditionalOnProperty)

更新时间:2023-06-03 07:22:52 阅读: 评论:0

springboot注解说明(@ConditionalOnProperty)springboot 注解说明(@ConditionalOnProperty)
应⽤:根据配置⽂件的属性决定是否将类注册为bean
**********************
相关类与注解
@ConditionalOnProperty:标注在类或者⽅法上
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({OnPropertyCondition.class})
public @interface ConditionalOnProperty {
String prefix() default "";    //prefix可不设置
String[] name() default {};
String[] value() default {};    //name、value有且只能设置⼀个,否则会报错
String havingValue() default "";
boolean matchIfMissing() default fal;
}
匹配规则:属性⽂件中查找key(prefix+"."+name)
属性⽂件中设置了key的值value:
ConditionalOnProperty设置了havingValue:value=havingvalue则匹配,若不等则不匹配
ConditionalOnproperty没有设置havingValue:value不等于fal则匹配,若为fal,则不匹配
在乎我的人
属性⽂件中没有设置key:
ConditionalOnProperty中matchingIfMissing:true匹配,fal不匹配
@Conditional
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
Class<? extends Condition>[] value();  //继承condition的类数组
早睡英语
}
Condition
@FunctionalInterface
public interface Condition {
boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}
OnPropertyCondition
@Order(-2147483608)
class OnPropertyCondition extends SpringBootCondition {
OnPropertyCondition() {
}
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
//获取匹配结果
List<AnnotationAttributes> allAnnotationAttributes = this.AllAnnotationAttributes(ConditionalOnProperty        List<ConditionMessage> noMatch = new ArrayList();
List<ConditionMessage> match = new ArrayList();
Iterator var6 = allAnnotationAttributes.iterator();
while(var6.hasNext()) {
AnnotationAttributes annotationAttributes = (();
ConditionOutcome outcome = this.determineOutcome(annotationAttributes, Environment());
鸽子汤怎么炖好喝又营养(outcome.isMatch() ? match : noMatch).ConditionMessage());
}
if (!noMatch.isEmpty()) {
Match(ConditionMessage.of(noMatch));
} el {
return ConditionOutcome.match(ConditionMessage.of(match));
}
}
private List<AnnotationAttributes> annotationAttributesFromMultiValueMap(MultiValueMap<String, Object> multiValueMap) {
//将属性转换为List<AnnotationAttributes>
List<Map<String, Object>> maps = new ArrayList();
multiValueMap.forEach((key, value) -> {
for(int i = 0; i < value.size(); ++i) {
Object map;
if (i < maps.size()) {
map = ((i);
} el {
map = new HashMap();
maps.add(map);
}
((Map)map).put(key, (i));
}
});
List<AnnotationAttributes> annotationAttributes = new ArrayList(maps.size());
Iterator var4 = maps.iterator();
while(var4.hasNext()) {
Map<String, Object> map = (();
annotationAttributes.add(AnnotationAttributes.fromMap(map));
return annotationAttributes;
}
private ConditionOutcome determineOutcome(AnnotationAttributes annotationAttributes, PropertyResolver resolver) {
OnPropertyCondition.Spec spec = new OnPropertyCondition.Spec(annotationAttributes);
List<String> missingProperties = new ArrayList();
List<String> nonMatchingProperties = new ArrayList();
if (!missingProperties.isEmpty()) {
Match(ConditionMessage.forCondition(ConditionalOnProperty.class, new Object[]{spec}).didNotFind("property", "properties").        } el {
return !nonMatchingProperties.isEmpty() ? Match(ConditionMessage.forCondit
ion(ConditionalOnProperty.class, new Object[]{spec})        }
}
private static class Spec {
private final String prefix;
private final String havingValue;
private final String[] names;
private final boolean matchIfMissing;
Spec(AnnotationAttributes annotationAttributes) {
String prefix = String("prefix").trim();
if (StringUtils.hasText(prefix) && !dsWith(".")) {
prefix = prefix + ".";
}
this.prefix = prefix;
this.havingValue = String("havingValue");孵小鸡
this.names = Names(annotationAttributes);
this.matchIfMissing = Boolean("matchIfMissing");
}
private String[] getNames(Map<String, Object> annotationAttributes) {
String[] value = (String[])((String[])("value"));
String[] name = (String[])((String[])("name"));
Asrt.state(value.length > 0 || name.length > 0, "The name or value attribute of @ConditionalOnProperty must be specified");
//name、value长度都为0,则抛出异常
Asrt.state(value.length == 0 || name.length == 0, "The name and value attributes of @ConditionalOnProperty are exclusive");
//name、value长度都⼤于0,则抛出异常
return value.length > 0 ? value : name;  //返回长度不为0的value或者name
}
private void collectProperties(PropertyResolver resolver, List<String> missing, List<String> nonMatching) {
String[] var4 = this.names;
int var5 = var4.length;
for(int var6 = 0; var6 < var5; ++var6) {
String name = var4[var6];
String key = this.prefix + name;      //key:prefix+"."+name
if (ainsProperty(key)) {
if (!this.Property(key), this.havingValue)) {
nonMatching.add(name);
}  //如果属性⽂件中含有key,
//havingValue不为null,key对应的值与havingValue不相等,最终判定不匹配
//havingValue为null,属性⽂件中key对应的值为fal,最终判定不匹配
} el if (!this.matchIfMissing) {
missing.add(name);
}      //如果属性⽂件中没有key,
//属性matchingIfMissing=true,最终判定匹配
//属性matchingIfMissing=fal,最终判定不匹配
}
private boolean isMatch(String value, String requiredValue) {
if (StringUtils.hasLength(requiredValue)) {
return requiredValue.equalsIgnoreCa(value); //如果设置了havingValue,value与havingValue相等则匹配,否则不匹配            } el {ipv6无网络访问权限
return !"fal".equalsIgnoreCa(value);
}  //如果没有设置havingValue,属性⽂件中key对应的value不为fal则匹配,为fal则不匹配
}
public String toString() {
StringBuilder result = new StringBuilder();
result.append("(");
result.append(this.prefix);
if (this.names.length == 1) {
result.append(this.names[0]);
} el {
result.append("[");
result.append(StringUtils.arrayToCommaDelimitedString(this.names));
result.append("]");
}
if (StringUtils.hasLength(this.havingValue)) {
result.append("=").append(this.havingValue);
}
result.append(")");
String();
}
}
}
**********************
⽰例
*******************
配置⽂件
person:
enabled: true
name: ⽠⽥李下
age: 20
student:
name: ⽠⽥李下2
age: 20
*******************
pojo 层
Person
@Data
@Component
@ConditionalOnProperty(prefix = "person",value = {"enabled"},havingValue = "true") @ConfigurationProperties("person")
public class Person {
private String name;
private Integer age;
}
Student
成绩不好@Data
@Componentsd卡驱动
@ConfigurationProperties(prefix = "student")
@ConditionalOnProperty(prefix = "student",value = "enabled",matchIfMissing = true) public class Student {
private String name;
private Integer age;
}
*******************
controller 层
HelloController
@RestController
public class HelloController {
@Resource
private Person person;
@Resource微店怎么开店铺
private Student student;
@RequestMapping("/hello")
public String hello(){
System.out.println(person);
System.out.println(student);
return "success";
}
}

本文发布于:2023-06-03 07:22:52,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/844419.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:属性   匹配   判定   设置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图