Slagathor
Slagathor

Reputation: 892

Spring @ConditionalOnExpression with OR statement

I have a @ConditionalOnExpression which uses a property from my application.properties. I would like the @ConditionalOnExpression to use a OR condition so that it triggers if one out three different values for the property is there.

@ConditionalOnExpression("'${env.name}'=='prod' or '${env.name}' == 'alsoProd'")

I cant seem to get this to work. So, is it even possible to use an OR statement in the @ConditionalOnExpression??

Upvotes: 2

Views: 9371

Answers (3)

ppiatkowski
ppiatkowski

Reputation: 37

Example from your question should work properly and it's valid with Spring's documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-operators-logical.

Are you sure that it doesn't work?

Upvotes: 0

Sven Döring
Sven Döring

Reputation: 4368

The Spring Expression Language (SpEL) allows definition of lists.

As you compare the same value twice you can simply define a list and check if the environment name is in that list.

@ConditionalOnExpression("{'prod', 'alsoProd'}.contains('${env.name}')")

Upvotes: 3

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

try this

@ConditionalOnExpression("'${env.name}'=='prod'", "'${env.name}' == 'alsoProd'")

Upvotes: 3

Related Questions