【问题标题】:How to run nested evaluations in Spring Expression Language如何在 Spring 表达式语言中运行嵌套求值
【发布时间】:2016-10-08 21:03:11
【问题描述】:

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

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

所以,在 SPeL 中,我尝试了类似的方法:

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

但这可以编译但不起作用。

如果 app.name=my-app,上述解析为字符串文字: my-app.feature.isEnabled

文字本身 id 没问题,但我实际上需要这个键的值。

如果我尝试用另一个表达式换行,它不会编译:

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

我尝试了上述的不同变体,但无法找到正确的公式。

这可能吗?

【问题讨论】:

标签: spring spring-el


【解决方案1】:

可能有一些更简单的东西,但这有效......

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

但是如果没有设置属性,你需要在属性占位符配置器上使用ignore-unresolvable="true"

【讨论】:

  • 这不起作用,因为 PropertyPlaceholderHelper 无法解析和识别内部嵌套属性并解析返回 endIndex -1(未找到)
  • 对我来说效果很好。我在发布之前对其进行了测试。你一定有错字什么的。
  • 我正在使用 spring boot 并尝试使用:@ConditionalOnExpression("#{'${${spring.application.name}.http.pingtFilter.isEnabled' != null && '$ {${spring.application.name}.http.pingtFilter.isEnabled'.toLowerCase().equals('false')}")
  • 您在每个 isEnabled 后都缺少右大括号。和我的比较一下。
  • 很好,感谢您的精彩回答。
猜你喜欢
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 2013-11-04
  • 1970-01-01
相关资源
最近更新 更多