配置Spring Boot通過@ConditionalOnProperty來控制Configuration是否生效
如下代碼:
@Bean
@ConditionalOnProperty(name = "xxx1", havingValue = "false", matchIfMissing = true)
public SecureProxyService secureProxyService() {
return new SecureProxyServiceImpl();
}
@Bean
@ConditionalOnProperty(name = "xxx1", havingValue = "true")
public SecureProxyService SecureProxyServiceImpl1() {
return new SecureProxyServiceImpl1();
}
根據配置信息,選擇使用接口的實現類,只有一個生效;
matchIfMissing--默認選擇的配置項,當配置為空時,matchIfMissing為true;
name--配置項的key,不存在時,返回false,存在時,和havingValue的值進行比較,相同值的配置生效;