Search code examples
springspring-el

How to run nested evaluations in Spring Expression Language


I want to Use SPeL and I need to evaluate a parameter from a configuration source. The problem is that the name/key is dynamic. So I rely on one parameter to resolve the other. I basically need to check a Boolean parameter.

Example: partial key/prefix: app.name full key: ${app.name}.feature.isEnabled

So, in SPeL I try something like:

#{'${app.name}.feature.isEnabled' != null && !'${app.name}.feature.isEnabled'}

But this compiles but doesn't work.

If app.name=my-app, the above resolves to string literal: my-app.feature.isEnabled

the literal in itself id ok but I actually need the value of this key.

If I try to wrap with another expression, it doesn't compile:

#{${'${app.name}.feature.isEnabled'} != null && !${'${app.name}.feature.isEnabled'}}

I tried different variations of the above but can't make it to the right formula.

Is this possible?


Solution

  • There might be something simpler, but this works...

    "#{'${${app.name}.feature.isEnabled}' != null ? '${${app.name}.feature.isEnabled}'.toLowerCase().equals('true') : 'false'}"
    

    But you need ignore-unresolvable="true" on the property placeholder configurer if the property is not set.