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

Viewed 18

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

features:
  - key: feature1
    enabled: true
    description: First feature
  - key: feature2
    enabled: false
    description: Second feature
...

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:

@ConditionalOnExpression("${features.?[key == 'feature1'][0].enabled}")

@ConditionalOnExpression("${features}.?[key == 'feature1'][0].enabled")

But both give the same error on startup:

org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'lcurly({)'

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?

0 Answers
Related