spring boot ConditionalOnProperty 和 ConditionalOnExpression 使用

1. spring boot ConditionalOnProperty 使用讲解

@Retention(RetentionPolicy.RUNTIME)  
@Target({ElementType.TYPE, ElementType.METHOD})  
@Documented  
@Conditional({OnPropertyCondition.class})  
public @interface ConditionalOnProperty {  
    String[] value() default {}; //数组,获取对应property名称的值,与name不可同时使用 

    String prefix() default "";//property名称的前缀,可有可无 

    String[] name() default {};//数组,property完整名称或部分名称(可与prefix组合使用,组成完整的property名称),与value不可同时使用 

    String havingValue() default "";//可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置 

    boolean matchIfMissing() default false;//缺少该property时是否可以加载。如果为true,没有该property也会正常加载;反之报错 

    boolean relaxedNames() default true;//是否可以松散匹配
}  

操作案例 案例1: 值必须匹配为123 才会有效 注意 : prefix 可以不用, 但是要写全部在name 上

@ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123")  
.yml配置如下:  
parentName:  
      sonName: 123      //正常 
parentName:  
      sonName: 1234     //失败,与havingValue给定的值不一致 

案例2: 值必须匹配为123 才会有效, 并且可以yml 或者 properties 文件中不设置这个属性, 因为matchIfMissing 为true

@ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123",matchIfMissing = true)  
// .yml配置如下: 
//不配置相关参数 //正常,当matchIfMissing = true时,即使没有该parentName.sonName属性也会加载正常 

案例3: 配置多个属性值, 并且属性值都是一样的情况下才有效 注意: 可以使用在判断两个配置属性都为为某个值的情况下使用, 比较方便, parentName.sonName和parentName.flag的值都要与havingValue的一致才行

@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123")
parentName:  
      sonName: 123  
      flag: 1234       //失败 
parentName:  
    sonName: 123  
    flag: 123        //正常 
parentName:  
    sonName: 123     //失败,缺少parentName.flag 

案例4: 配置多个属性值, 并且属性值都是一样的情况下才有效, 其中设置 matchIfMissing = true, 允许不在配置文件中出现

@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123",matchIfMissing = true)
parentName:  
    sonName: 123     //正常 

// .yml配置如下: 
// 不配置相关参数 //正常 

2. ConditionalOnExpression 使用详解 Solution for Two Properties

@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")


注意: 上述的英文解释, 来源于参考地址, 在下方
Note the following:

You need to using colon notation to indicate the default value of the property in the expression language statement Each property is in a separate expression language block ${} The && operator is used outside of the SpEL blocks Solution for more then 2 properties

@ConditionalOnExpression("${properties.first.property.enable:true} " +
        "&& ${properties.second.property.enable:true} " +
        "&& ${properties.third.property.enable:true}")


注意: 原文解释, 的一些缺点。

The drawback is that you cannot use a matchIfMissing argument as you would be able to when using the @ConditionalOnProperty annotation so you will have to ensure that the properties are present in the .properties or YAML files for all your profiles/environments or just rely on the default value other so solution

@ConditionalOnExpression("'${com.property1}'.equals('${com.property2}')")



https://blog.csdn.net/u010002184/article/details/79353696
https://stackoverflow.com/questions/40477251/spring-boot-spel-conditionalonexpression-check-multiple-properties

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索spring , boot , 注解使用 , ConditionalOnExpression ConditionalOnProperty ,以便于您获取更多的相关知识。

猜你喜欢

转载自blog.csdn.net/ljz1315/article/details/84400420