本文介绍了如何在Spring Expression Language中运行嵌套评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SPeL,并且需要从配置源评估参数.问题在于名称/键是动态的.所以我依靠一个参数来解决另一个.我基本上需要检查一个布尔参数.

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.

示例:部分键/前缀:app.name全键:$ {app.name} .feature.isEnabled

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

因此,在SPeL中,我尝试以下操作:

So, in SPeL I try something like:

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

但是这可以编译,但是不起作用.

But this compiles but doesn't work.

如果app.name = my-app,则上面的内容解析为字符串文字:my-app.feature.isEnabled

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.

这可能吗?

推荐答案

可能更简单一些,但这可行...

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

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

但是,如果未设置属性,则需要在属性占位符配置程序上使用ignore-unresolvable="true".

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

这篇关于如何在Spring Expression Language中运行嵌套评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 04:07