选择性配置-ConditionalOnProperty

配置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的值进行比较,相同值的配置生效;

原文地址:https://www.cnblogs.com/chenglc/p/12841215.html