2

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?

YaOg
  • 1,748
  • 5
  • 24
  • 43
  • Possible duplicate of [How to evaluate a dynamic/nested Spring property placeholder expression?](http://stackoverflow.com/questions/20705322/how-to-evaluate-a-dynamic-nested-spring-property-placeholder-expression) – Steve Chambers Nov 03 '16 at 09:04

1 Answers1

7

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • This doesn't work as PropertyPlaceholderHelper fails to parse and identify the inner nested property and parse return endIndex -1 (not found) – YaOg Jun 13 '16 at 07:53
  • It worked fine for me. I tested it before posting. You must have a typo or something. – Gary Russell Jun 13 '16 at 07:56
  • I am using spring boot and trying to apply it using: @ConditionalOnExpression("#{'${${spring.application.name}.http.pingtFilter.isEnabled' != null && '${${spring.application.name}.http.pingtFilter.isEnabled'.toLowerCase().equals('false')}") – YaOg Jun 13 '16 at 12:23
  • You're missing closing braces after each isEnabled. Compare with mine. – Gary Russell Jun 13 '16 at 13:47
  • Great catch, Thanks for a great Answer. – YaOg Jun 14 '16 at 04:31