Java 如何在 Spring @Value 注释中正确指定默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26916598/
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
How to correctly specify a default value in the Spring @Value annotation?
提问by Alex
Initially, I have the following spec:
最初,我有以下规范:
@Value("#{props.isFPL}")
private boolean isFPL=false;
This works fine correctly getting the value from the property file:
这可以正常从属性文件中正确获取值:
isFPL = true
However, the following expression with default results in the error:
但是,具有默认值的以下表达式会导致错误:
@Value("#{props.isFPL:false}")
private boolean isFPL=false;
Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1041E:(pos 28): After parsing a valid expression, there is still more data in the expression: 'colon(:)'
表达式解析失败;嵌套异常是 org.springframework.expression.spel.SpelParseException: EL1041E:(pos 28): 解析有效表达式后,表达式中还有更多数据:'colon(:)'
I also tried to use $ instead of #.
我也尝试使用 $ 而不是 #。
@Value("${props.isFPL:true}")
private boolean isFPL=false;
Then the default value in annotation works fine but I did not get the correct value from the Properties file:
然后注释中的默认值工作正常,但我没有从属性文件中获得正确的值:
采纳答案by shi9
Try with $
as follows:
尝试$
如下:
@Value("${props.isFPL:true}")
private boolean isFPL=false;
Also make sure you set the ignore-resource-no-found
to trueso that if the property file is missing, the defaultvalue will be taken.
还要确保将 设置ignore-resource-no-found
为true,以便如果缺少属性文件,将采用默认值。
Place the following in the context file if using XML based configuration:
如果使用基于 XML 的配置,请将以下内容放在上下文文件中:
<context:property-placeholder ignore-resource-not-found="true"/>
If using Java configurations:
如果使用 Java 配置:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
p.setIgnoreResourceNotFound(true);
return p;
}
回答by Mike Summers
Depends on how you're loading your properties, if you use something like
取决于你如何加载你的属性,如果你使用类似的东西
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
Then @Value
should look like
然后@Value
应该看起来像
@Value("${isFPL:true}")
回答by Kevin
Does your Spring application context file contains more than one propertyPlaceholder beans like below:
您的 Spring 应用程序上下文文件是否包含多个 propertyPlaceholder bean,如下所示:
<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true" location="classpath*:/*.local.properties" />
<context:property-placeholder location="classpath:/config.properties" />
If so, then property lookup for: props.isFPLwill only take place for the first property file (.local.properties), if property not found, the default value (true) will take effect and the second property file (config.properties) is effectively ignored for this property.
如果是这样,则属性查找:props.isFPL将只针对第一个属性文件(.local.properties)进行,如果未找到属性,则默认值(true)将生效,第二个属性文件(config.properties )) 被此属性有效地忽略。
回答by Kevin Liu
For int
type variable:
对于int
类型变量:
@Value("${my.int.config: #{100}}")
int myIntConfig;
Note:there is no space beforethe colon, but an extra space afterthe colon.
注意:冒号前没有空格,冒号后多了一个空格。
回答by Dave Richardson
For a String you can default to null like so:
对于字符串,您可以像这样默认为 null:
public UrlTester(@Value("${testUrl:}") String url) {
this.url = url;
}