The @ConditionalOnProperty and @ConditionalOnExpression annotations do not have java.lang.annotation.Repeatable annotations, so you cannot just add multiple annotations to check for multiple properties.
The following syntax has been tested and works:
Solution for two properties
@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")
Please note the following:
- You need to use a colon to indicate the default property in the expression language expression
- Each property is in a separate language block of the expression $ {}
- && operator used outside SpEL blocks
It allows you to use multiple properties with different values ββand can apply to multiple properties.
If you want to check more than two values ββand still maintain readability, you can use the concatenation operator between the various conditions that you evaluate:
Solution for more than 2 properties
@ConditionalOnExpression("${properties.first.property.enable:true} " + "&& ${properties.second.property.enable:true} " + "&& ${properties.third.property.enable:true}")
The downside is that you cannot use the matchIfMissing argument, as you could use the @ConditionalOnProperty annotation, so you will need to make sure that the properties are present in the .properties or YAML files for all your profiles / environments or just rely on the default value
source share