简体   繁体   English

如何在 Spring @Value 注释中正确指定默认值?

[英]How to correctly specify a default value in the Spring @Value annotation?

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:然后注释中的默认值工作正常,但我没有从属性文件中获得正确的值:

Try with $ as follows:尝试使用$如下:

@Value("${props.isFPL:true}")
private boolean isFPL=false;

Also make sure you set the ignore-resource-no-found to true so that if the property file is missing, the default value will be taken.还要确保将ignore-resource-no-foundtrue,以便如果缺少属性文件,将采用默认值。

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;
 }

For int type variable:对于int类型变量:

@Value("${my.int.config: #{100}}")
int myIntConfig;

Note: there is no space before the colon, but an extra space after the colon.注意:冒号没有空格,冒号后多了一个空格。

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.isFPL will 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 ) ) 被此属性有效地忽略。

For a String you can default to null like so:对于字符串,您可以像这样默认为 null:

public UrlTester(@Value("${testUrl:}") String url) {
    this.url = url;
}

Depends on how you're loading your properties, if you use something like取决于你如何加载你的属性,如果你使用类似的东西

<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />

Then @Value should look like然后@Value应该看起来像

@Value("${isFPL:true}")

The exact answer of your question depends on the type of the parameter.您问题的确切答案取决于参数的类型

for 'String' parameters, your sample code works fine:对于“字符串”参数,您的示例代码工作正常:

@Value("#{props.string.fpl:test}")
private String fpl = "test";

for other types (like boolean in your question) it should be written in this way:对于其他类型(如您问题中的布尔值),它应该这样写:

@Value("${props.boolean.isFPL:#{false}}")
private boolean isFPL = false;

or for 'Integers':或对于“整数”:

@Value("${props.integer.fpl:#{20}}")

This way of defining defaults works only if we write "value=..." in the @Value annotation.这种定义默认值的方式只有在我们在@Value 注释中写入“value=...”时才有效。 eg例如

Does not work : @Value("${testUrl:some-url}" // This will always set "some-url" no matter what you do in the config file.不起作用: @Value("${testUrl:some-url}" // 无论您在配置文件中做什么,这都会始终设置“some-url”。

Works : @Value(value = "${testUrl:some-url}" // This will set "some-url" only if testUrl property is missing in the config file.有效:@Value(value = "${testUrl:some-url}" // 仅当配置文件中缺少 testUrl 属性时才会设置“some-url”。

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

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