Skip to content
Advertisement

Spring Boot: using SpEL collection selection from YAML in @ConditionalOnExpression

I have a Spring Boot application with a YAML configuration that contains a feature list like this:

JavaScript

I would like to use @ConditionalOnExpression to conditionally initialize beans related to those features, identifying them by keys. Since “features” property is a list, it seems I need collection selection to do this. I have tried these two options for the annotation’s value:

JavaScript

But both give the same error on startup:

JavaScript

If I pass the expression (without ${}) to SpelExpressionParser.parseExpression() and then evaluate it (against a list of feature objects built programmatically), it works as expected and returns the value of “enabled” property. So the expression’s structure seems to be OK, and the problem is how I use it in @ConditionalOnExpression. What exactly can I be doing wrong?

Advertisement

Answer

Spring SpEL supports only simple property placeholders inside the properties file.

If your properties file and condition code are like this, your SpEL will work:

JavaScript
JavaScript
But if you want to load it as a map feature and use the map SpEL features;

The most elegant way is writing the key-values as an inline JSON (use ‘ and ” chars to avoid cumbersome escaping) and parsing it using SpEL:

JavaScript
JavaScript

If you had used the selection method it would have looked like this;

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement