@ConditionalOnProperty注解详解

时间:2025-04-04 07:07:02
// 对应的测试注入情况
@Configuration
@ConditionalOnProperty(value = "")
public class parentNameConfig {
}
.yml配置如下:
parentName:
    sonName: true     //正常
parentName:
    sonName:          //正常,空字符时    
parentName:
    sonName: false       //失败
parentName:
    sonName: null      //正常
parentName:
    sonName: 123      //正常
    
把第二行换成:
@ConditionalOnProperty(value = "",havingValue = "123")    
.yml配置如下:
parentName:
    sonName: 123      //正常
parentName:
    sonName: 1234      //失败
parentName:
    sonName: false      //失败
    
把第二行换成:
@ConditionalOnProperty(value = "",havingValue = "false")    
.yml配置如下:
parentName:
    sonName: false      //正常
    
把第二行换成:
@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时,即使没有该属性也会加载正常
 
 
把第二行换成:
@ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123",matchIfMissing = false) //这里matchIfMissing默认为false,写不写都行
.yml配置如下:    
不配置相关参数       //失败,当matchIfMissing = false时,必须要有对应的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中的属性需要两个都存在且都不为false才会加载正常
.yml配置如下:    
parentName:
    sonName: true
    flag: true       //正常
parentName:
    sonName: true
    flag: 123        //正常
parentName:
    sonName: true
    flag: false      //失败    
parentName:
    sonName: false
    flag: false      //失败    
 
把第二行换成:
@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "false")
parentName:
    sonName: false
    flag: false      //正常    
 
把第二行换成:
@ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123")//和的值都要与havingValue的一致才行
parentName:
    sonName: 123
    flag: 1234       //失败    
parentName:
    sonName: 123
    flag: 123         //正常
parentName:
    sonName: 123     //失败,缺少
    
把第二行换成:    
@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配置如下:    
不配置相关参数      //失败