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

相关文章:

  • 2021-12-22
  • 2021-04-19
  • 2021-09-27
  • 2021-12-15
  • 2021-12-13
  • 2021-11-16
  • 2021-10-12
  • 2022-12-23
猜你喜欢
  • 2021-07-16
  • 2022-02-16
  • 2023-04-06
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
相关资源
相似解决方案