繁体   English   中英

Spring Value注释默认为表达式?

[英]Spring Value annotation default as expression?

考虑file的以下属性

defaultProperty="default"
overrideProperty="override"

我有一个类,首先需要检查是否可以使用overrideProperty并使用它,否则将返回defaultProperty

我尝试这样做

MyClass {

 @Value("${overrideProperty: defaultProperty}")
 String tokenizationAlgorithm;
}

这不起作用,因为我得到的是defaultProperty不是 default (这是值)

如何将表达式放在@Value批注的默认值部分中?

使用以下语法:

@Value("${overrideProperty:${defaultProperty}}")

${overrideProperty:${defaultProperty}}将传递给PropertyPlaceholderHelperreplacePlaceholders方法,该方法将用其对应的值替换格式${name}所有占位符。

请注意,由于parseStringValue方法的递归性质, ${defaultProperty}值将在${overrideProperty}之前解析。 因此,即使提供了${overrideProperty} ,您也应该始终使用默认值。 另外,您可以提供默认值作为默认值!

@Value("${overrideProperty:${defaultProperty:literallyDefault}}")

另外, 注意差距

@Value("${overrideProperty: defaultProperty}")
                           ^ This space will be placed before your default value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM