How to run nested evaluations in Spring Expression Language

北战南征 提交于 2019-12-04 04:49:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37705134/how-to-run-nested-evaluations-in-spring-expression-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!