Spring Boot SpEL ConditionalOnExpression check multiple properties

30,158

Solution 1

The annotations @ConditionalOnProperty and @ConditionalOnExpression both do NOT have the java.lang.annotation.Repeatable annotation so you would not be able to just add multiple annotations for checking 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}")

Note the following:

  • You need to using colon notation to indicate the default value of the property in the expression language statement
  • Each property is in a separate expression language block ${}
  • The && operator is used outside of the SpEL blocks

It allows for multiple properties that have differing values and can extend to multiple properties.

If you want to check more then 2 values and still maintain readability, you can use the concatenation operator between different conditions you are evaluating:

Solution for more then 2 properties

@ConditionalOnExpression("${properties.first.property.enable:true} " +
        "&& ${properties.second.property.enable:true} " +
        "&& ${properties.third.property.enable:true}")

The drawback is that you cannot use a matchIfMissing argument as you would be able to when using the @ConditionalOnProperty annotation so you will have to ensure that the properties are present in the .properties or YAML files for all your profiles/environments or just rely on the default value

Solution 2

As far as i remember, You could use this kind of expression:

@ConditionalOnExpression("'${com.property1}'.equals('${com.property2}')")

for further reading here is the link

if it was helpful, please do comment so that my confusion may also get cleared.

Share:
30,158
anataliocs
Author by

anataliocs

Veteran software engineer with over 12 years of experience designing and implementing scalable, distributed systems and technical educator. Platform Advocate for ConsenSys performing devrel for the Infura platform. chris-anatalio.eth LinkedIn Learning Courses Building a Full-Stack App with Angular 2+ and Spring Boot JHipster: Build and DeploySpring Boot Microservices Reactive Java 8 Reactive Spring Building a Reactive App with Angular and SpringBoot 2

Updated on February 09, 2020

Comments

  • anataliocs
    anataliocs about 4 years

    Question:

    How can I use Spring Expression Language to check that 2 Boolean properties are true?

    For example, checking that a single property is true would use the syntax:

    Example

    @ConditionalOnExpression("${property.from.properties.file}")
    

    What would be the syntax for checking property1 == true && property2 == false? Where the properties can potentially have different values.

    The answer from a similar question: How to check two condition while using @ConditionalOnProperty or @ConditionalOnExpression concatenates two strings together and performs a check like so:

    Concatenation Solution

    @ConditionalOnExpression("'${com.property1}${com.property2}'=='value1value2'")
    

    That syntax seems confusing to someone reading that code and it seems like a hacky solution. There are some edge cases where the solution fails as well. I want to find the proper way to check two separate properties without concatenating the values.

    Note: Also just to be clear, the answer isn't something you can easily search for from what I've seen. It seems like it would be a really simple answer but it's proving to be fairly elusive.

  • anataliocs
    anataliocs over 7 years
    Thanks for the response, but I also need to handle the case where the properties are potentially different values
  • Frankie Drake
    Frankie Drake about 4 years
    Assigning the default value really matters. If value is null, then even if you wanna check it for null, you should tell that missing value should be interpreted as null, otherwise it will fail with an SPeL parse error. Thank you.