java Spring Boot SpEL ConditionalOnExpression 检查多个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40477251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:14:47  来源:igfitidea点击:

Spring Boot SpEL ConditionalOnExpression check multiple properties

javaspringspring-bootspring-el

提问by anataliocs

Question:

问题:

How can I use Spring Expression Language to check that 2 Boolean properties are true?

如何使用 Spring 表达式语言检查 2 个布尔属性是否为真?

For example, checking that a single property is true would use the syntax:

例如,检查单个属性是否为真将使用以下语法:

Example

例子

@ConditionalOnExpression("${property.from.properties.file}")

What would be the syntax for checking property1 == true && property2 == false? Where the properties can potentially have different values.

检查的语法是property1 == true && property2 == false什么?属性可能具有不同值的地方。

The answer from a similar question: How to check two condition while using @ConditionalOnProperty or @ConditionalOnExpressionconcatenates two strings together and performs a check like so:

来自类似问题的答案:如何在使用 @ConditionalOnProperty 或 @ConditionalOnExpression 时检查两个条件将两个字符串连接在一起并执行如下检查:

Concatenation Solution

串联方案

@ConditionalOnExpression("'${com.property1}${com.property2}'=='value1value2'")

That syntax seems confusing to someone reading that code and it seems like a hacky solution. There are some edge cases where the solution fails as well. I want to find the proper way to check two separate properties without concatenating the values.

对于阅读该代码的人来说,这种语法似乎令人困惑,而且它似乎是一个笨拙的解决方案。在某些边缘情况下,解决方案也会失败。我想找到在不连接值的情况下检查两个单独属性的正确方法。

Note:Also just to be clear, the answer isn't something you can easily search for from what I've seen. It seems like it would be a really simple answer but it's proving to be fairly elusive.

注意:同样要明确的是,答案不是您可以根据我所看到的轻松搜索的内容。这似乎是一个非常简单的答案,但事实证明它相当难以捉摸。

回答by anataliocs

The annotations @ConditionalOnPropertyand @ConditionalOnExpressionboth do NOT have the java.lang.annotation.Repeatableannotation so you would not be able to just add multiple annotations for checking multiple properties.

注释@ConditionalOnProperty@ConditionalOnExpression两者都没有java.lang.annotation.Repeatable注释,因此您无法仅添加多个注释来检查多个属性。

The following syntax has been tested and works:

以下语法已经过测试并且有效:

Solution for Two Properties

两个属性的解决方案

@ConditionalOnExpression("${properties.first.property.enable:true} && ${properties.second.property.startServer:false}")

Note the following:

请注意以下事项:

  • You need to using colon notation to indicate the default value of the property in the expression language statement
  • Each property is in a separate expression language block ${}
  • The && operator is used outside of the SpEL blocks
  • 表达式语言语句中需要使用冒号表示属性的默认值
  • 每个属性都在一个单独的表达式语言块 ${}
  • && 运算符在 SpEL 块之外使用

It allows for multiple properties that have differing values and can extend to multiple properties.

它允许具有不同值的多个属性并且可以扩展到多个属性。

If you want to check more then 2 values and still maintain readability, you can use the concatenation operator between different conditions you are evaluating:

如果要检查多于 2 个值并仍保持可读性,可以在要评估的不同条件之间使用连接运算符:

Solution for more then 2 properties

超过 2 个属性的解决方案

@ConditionalOnExpression("${properties.first.property.enable:true} " +
        "&& ${properties.second.property.enable:true} " +
        "&& ${properties.third.property.enable:true}")

The drawback is that you cannot use a matchIfMissing argument as you would be able to when using the @ConditionalOnPropertyannotation so you will have to ensure that the properties are present in the .propertiesor YAMLfiles for all your profiles/environments or just rely on the default value

缺点是您不能像使用注释一样使用 matchIfMissing 参数,@ConditionalOnProperty因此您必须确保所有配置文件/环境的.propertiesYAML文件中都存在这些属性,或者仅依赖默认值价值

回答by Tahir Hussain Mir

As far as i remember, You could use this kind of expression:

据我所知,您可以使用这种表达方式:

@ConditionalOnExpression("'${com.property1}'.equals('${com.property2}')")

for further reading here is the link

进一步阅读这里是链接

if it was helpful, please do comment so that my confusion may also get cleared.

如果有帮助,请发表评论,以便我的困惑也能得到澄清。