ConditionalOnProperty


配置Spring Boot通过@ConditionalOnProperty来控制Configuration是否生效

1、matchIfMissing属性:从application.properties中读取某个属性值,如果该值为空,默认值为true

@Configuration
@ConditionalOnClass({ Feign.class }) @ConditionalOnProperty(value = "feign.oauth2.enabled", matchIfMissing = true) public class OAuth2FeignAutoConfiguration { @Bean @ConditionalOnBean(OAuth2ClientContext.class) public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) { return new OAuth2FeignRequestInterceptor(oauth2ClientContext); } }

 

2、havingValue属性:通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。

@Configuration
//如果synchronize在配置文件中并且值为true
@ConditionalOnProperty(name = "synchronize", havingValue = "true")
public class SecondDatasourceConfig {

    @Bean(name = "SecondDataSource")
    @Qualifier("SecondDataSource")
    @ConfigurationProperties(prefix = "spring.second.datasource")
    public DataSource jwcDataSource() {
        return DataSourceBuilder.create().build();
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM