@ConditionalOnProperty控制@Configuration是否生效


在網上查詢@ConditionalOnProperty的使用的時候,發現好多類似的文章,但是例子都不夠全面。這里記錄下測試示例,方便自己記憶。

1、簡介

   SpringBoot通過@ConditionalOnProperty來控制@Configuration是否生效

 

2、說明

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {

    String[] value() default {}; //數組,獲取對應property名稱的值,與name不可同時使用  
  
    String prefix() default "";//property名稱的前綴(最后一個屬性前的一串。比如aaa.bbb.ccc,則prefix為aaa.bbb),可有可無  
  
    String[] name() default {};//數組,property完整名稱或部分名稱(可與prefix組合使用,組成完整的property名稱),與value不可同時使用  
  
    String havingValue() default "";//可與name組合使用,比較獲取到的屬性值與havingValue給定的值是否相同,相同才加載配置  
  
    boolean matchIfMissing() default false;//缺省配置。配置文件沒有配置時,表示使用當前property。配置文件與havingValue匹配時,也使用當前property
  
    boolean relaxedNames() default true;//是否可以松散匹配,至今不知道怎么使用的  
}

 

3、單個示例

 a、建立一個Configuration配置類:

@Data
@Configuration
@ConditionalOnProperty(value = "test.conditional", havingValue = "abc")
public class TestConfiguration {

    /** 默認值 */
    private String field = "default";

}

 b、建立一個測試類:

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired(required = false)
    private TestConfiguration test1Configuration;

    @GetMapping("testConfiguration")
    public String testConfiguration() {
        if (test1Configuration != null) {
            return test1Configuration.getField();
        }
        return "OK";
    }

}

c、配置文件application.yml:

test:
  conditional: abc

 d、通過postman或者其它工具發送請求。結果如下:

default

以上表明TestConfiguration配置文件生效了

 

4、多種示例

接下來,改變@ConditionalOnProperty中的各個屬性,然后通過查看返回結果來判斷TestConfiguration是否生效。

1、不配置@ConditionalOnProperty,直接生效。

2、只有value屬性,沒有havingValue屬性。如果application.yml配置了test.conditional則生效,否則不生效。

@ConditionalOnProperty(value = "test.conditional")
test:
  conditional: edf

3、prefix + name相當於value屬性(兩者不可同時使用)。如果application.yml配置了test.conditional則生效,否則不生效

@ConditionalOnProperty(prefix = "test", name = "conditional")
test:
  conditional: edf

4、name屬性為一個數組,當要匹配多個值時,如果application.yml的配置與name屬性中的值一一匹配則生效,否則不生效

@ConditionalOnProperty(prefix = "test", name = { "conditional", "other" })
test:
  conditional: abc
  other: edf

5、當matchIfMissing=true時:

 a、如果application.yml配置了test.conditional則生效(此時matchIfMissing可有可無),否則不生效

 b、如果application.yml啥都沒配置則生效

@ConditionalOnProperty(prefix = "test", name = "conditional", matchIfMissing = true)

6、加上havingValue屬性。當havingValue的值與application.yml文件中test.conditional的值一致時則生效,否則不生效

@ConditionalOnProperty(prefix = "test", name = "conditional", havingValue = "abc")
test:
  conditional: abc

7、加上havingValue屬性。name屬性為數組時,如果application.yml文件中配置了相關屬性且值都一致時則生效,否則不生效

@ConditionalOnProperty(prefix = "test", name = { "conditional", "other" }, havingValue = "abc")
test:
  conditional: abc
  other: abc

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM