How to check two condition while using @ConditionalOnProperty or @ConditionalOnExpression

Zenith Kenneth picture Zenith Kenneth · Mar 15, 2016 · Viewed 28.5k times · Source

I need to check that two conditions are satisfied on a YAML property file, while creating a bean. How do I do that, as the @ConditionalOnProperty annotation supports only one property?

Answer

Josh picture Josh · Aug 23, 2017

Since from the beginning of @ConditionalOnProperty it was possible to check more than one property. The name / value attribute is an array.

@Configuration
@ConditionalOnProperty({ "property1", "property2" })
protected static class MultiplePropertiesRequiredConfiguration {

    @Bean
    public String foo() {
        return "foo";
    }

}

For simple boolean properties with an AND check you don't need a @ConditionalOnExpression.