@ConditionalOnProperty注解详解
Spring boot中的注解@ConditionalOnProperty,可以通过配置⽂件中的属性值来判定configuration是否被注⼊
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
中医治疗尿毒症/**
* String数组该属性与下⾯的 name 属性不可同时使⽤,
* 当value所对应配置⽂件中的值为fal时,注⼊不⽣效,不为fasle注⼊⽣效
* value有多个值时,只要有⼀个值对应为fal,则注⼊不成功
*/
String[] value() default {};
/**
* 配置⽂件中key的前缀,可与value 或 name 组合使⽤
*/
String prefix() default "";
/**
* 与 value 作⽤⼀致
*/
String[] name() default {};
/**
* 与value 或 name 组合使⽤,只有当value 或 name 对应的值与havingValue的值相同时,注⼊⽣效 */ String havingValue() default ""; /** * 配置中缺少对应的属性时, */
String havingValue() default "";
/**
*该属性为true时,配置⽂件中缺少对应的value或name的对应的属性值,也会注⼊成功
*/
boolean matchIfMissing() default fal;
// 对应的测试注⼊情况
@Configuration
@ConditionalOnProperty(value = "parentName.sonName")
public class parentNameConfig {
}
.yml配置如下:
parentName:
sonName: true //正常
parentName:
sonName: //正常,空字符时
parentName:
sonName: fal //失败
parentName:
sonName: null //正常
parentName:
sonName: 123 //正常
把第⼆⾏换成:
@ConditionalOnProperty(value = "parentName.sonName",havingValue = "123")
.yml配置如下:
parentName:
parentName:
sonName: 123 //正常
parentName:
sonName: 1234 //失败
parentName:
sonName: fal //失败
把第⼆⾏换成:
@ConditionalOnProperty(value = "parentName.sonName",havingValue = "fal")
.yml配置如下:
parentName:
sonName: fal //正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123")
.yml配置如下:
parentName:
sonName: 123 //正常
parentName:
sonName: 1234 //失败,与havingValue给定的值不⼀致
十大品牌家具
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123",matchIfMissing = true)
.yml配置如下:
不配置相关参数 //正常,当matchIfMissing = true时,即使没有该parentName.sonName属性也会加载正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123",matchIfMissing = fal) //这⾥matchIfMissing默认为fal,写不写都⾏
.yml配置如下:
不配置相关参数 //失败,当matchIfMissing = fal时,必须要有对应的property
parentName:
sonName: 123 //正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName.", name = "sonName")//prefix带了.(点)
.yml配置如下:
围巾什么牌子好
parentName:
sonName: true //正常
parentName:
sonName: 123 //正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName",value = {"sonName2"},name = {"sonName"})
.yml配置如下:
发热是什么症状
parentName:
sonName: true //项⽬Debug启动失败,The name and value attributes of @ConditionalOnProperty are exclusive
//@ConditionalOnProperty的name和value属性是互斥的,不能同时出现
把第⼆⾏换成:
网店产品@ConditionalOnProperty(prefix = "parentName",name = {"sonName"})
.yml配置如下:
parentName:
sonName: true //正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName",name = {"sonName","flag"}) //name中的属性需要两个都存在且都不为fal才会加载正常
.yml配置如下:
parentName:
sonName: true
flag: true //正常
parentName:
sonName: true
flag: 123 //正常
parentName:
sonName: true
flag: fal //失败
parentName:
sonName: fal
flag: fal //失败
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "fal")
大雄性parentName:
sonName: fal
flag: fal //正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123")//parentName.sonName 和parentName.flag的值都要与havingValue的⼀致才⾏
parentName:
sonName: 123
flag: 1234 //失败
parentName:
sonName: 123
flag: 123 //正常
parentName:
sonName: 123 //失败,缺少parentName.flag
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123",matchIfMissing =
怎么换手机壁纸true)//matchIfMissing = true允许缺少
parentName:
sonName: 123 //正常
摩门经
.yml配置如下:
不配置相关参数 //正常
把第⼆⾏换成:
@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123")
.yml配置如下:
不配置相关参数 //失败