Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 311
YaOg Avatar asked Jan 25 '26 09:01

YaOg


1 Answers

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.

like image 79
Gary Russell Avatar answered Jan 26 '26 23:01

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!