ConditionalOnProperty

配置Spring Boot經過@ConditionalOnProperty來控制Configuration是否生效spring

一、matchIfMissing屬性:從application.properties中讀取某個屬性值,若是該值爲空,默認值爲trueapp

@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); } }

 

二、havingValue屬性:經過其兩個屬性name以及havingValue來實現的,其中name用來從application.properties中讀取某個屬性值,若是該值爲空,則返回false;若是值不爲空,則將該值與havingValue指定的值進行比較,若是同樣則返回true;不然返回false。若是返回值爲false,則該configuration不生效;爲true則生效。ui

@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();
    }
}
相關文章
相關標籤/搜索