1

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??

Slagathor
  • 852
  • 7
  • 23

3 Answers3

3

try this

@ConditionalOnExpression("'${env.name}'=='prod'", "'${env.name}' == 'alsoProd'")
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
2

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}')")
Sven Döring
  • 3,927
  • 2
  • 14
  • 17
0

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?