java Spring Boot:Spring 总是为属性分配默认值,尽管它存在于 .properties 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28369582/
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-02 13:27:50  来源:igfitidea点击:

Spring Boot : Spring always assigns default value to property despite of it being present in .properties file

javaspringspring-bootspring-annotations

提问by Darshan Mehta

I am working with Spring boot 1.1.8 which uses Spring 4.0.7. I am autowiring the properties in my classes with @Value annotation. I want to have a default value if the property is not present in properties file so, I use ":" to assign default value. Below is the example:

我正在使用使用 Spring 4.0.7 的 Spring boot 1.1.8。我正在使用 @Value 注释自动装配我的类中的属性。如果属性文件中不存在该属性,我希望有一个默认值,因此,我使用“:”来分配默认值。下面是示例:

@Value("${custom.data.export:false}")
private boolean exportData = true;

It should assign false to the variable if property is not present in the properties file which is does. However, ifproperty is present in the file, then also it assigns default value and ignores the properties value. E.g. if I have defined the property like the one mentioned above and application properties file has something like this custom.data.export=truethen, the value of exportDatawill stillbe false whereas it should be true ideally.

如果属性在属性文件中不存在,它应该为变量分配 false。但是,如果文件中存在属性,那么它也会分配默认值并忽略属性值。例如,如果我定义了诸如上述应用属性文件中提到的一个属性有这样的事情custom.data.export=true的话,价值exportData仍然是假的,而应该是真正的理想。

Can anyone please guide me what I am doing wrong here?

谁能指导我我在这里做错了什么?

Thanks

谢谢

回答by Ophir Radnitz

We were bitten by the following Spring bug with exactly the same symptom:

我们被以下具有完全相同症状的 Spring 错误所咬:

[SPR-9989] Using multiple PropertyPlaceholderConfigurer breaks @Value default value behavior

[SPR-9989] 使用多个 PropertyPlaceholderConfigurer 会破坏 @Value 默认值行为

Basically if more than a single PropertyPlaceholderConfigureris present in the ApplicationContext, only predefined defaults will be resolved and no overrides will take place. Setting a different ignoreUnresolvablePlaceholdersvalue had no impact on the matter, and both values (true/false) worked equally well in that regard once we removed the extra PropertyPlaceholderConfigurer.

基本上,如果PropertyPlaceholderConfigurerApplicationContext 中存在多个,则只会解析预定义的默认值并且不会发生覆盖。设置不同的ignoreUnresolvablePlaceholders值对此事没有影响,一旦我们删除了额外的PropertyPlaceholderConfigurer.

Looking into it, each of the defined PropertyPlaceholderConfigurerinternally resolved the properties as expected, but Spring couldn't figure out which of them to use in order to inject a value into the @Valueannotated fields/params.

查看它,每个PropertyPlaceholderConfigurer内部定义的属性都按预期解析,但 Spring 无法确定使用哪个属性来将值注入带@Value注释的字段/参数。

回答by Arpit Jain

You can do one of the following to overcome this:

您可以执行以下操作之一来克服此问题:

  1. Use custom valueSeparator in your configurer
  1. 在配置器中使用自定义 valueSeparator

<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location" value="file:${catalina.base}/conf/config2.properties"/>
     <property name="ignoreUnresolvablePlaceholders" value="true"/>
     <property name="valueSeparator" value="-defVal-"/>
</bean>

  1. Increase the preference of the relevant configurer using the order property
  1. 使用 order 属性增加相关配置器的偏好

<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:${catalina.base}/conf/config2.properties"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="order" value="-2147483648"/>
</bean?

I have done some RnD on this issue, available here.

我在这个问题上做了一些研究,可在此处获得

回答by justAnotherUser...

As @Ophir Radnitz stated, this is a spring bug that happens when there is more than one PropertyPlaceholderConfigurer present in the ApplicationContext.

正如@Ophir Radnitz 所说,这是一个弹簧错误,当 ApplicationContext 中存在多个 PropertyPlaceholderConfigurer 时会发生这种错误。

As a workaround, you can obtain the desired behavior with something like that:

作为一种解决方法,您可以通过以下方式获得所需的行为:

(...)

@Autowired
private Environment environment;

(...)

private Boolean shouldExportData()
{        
    return environment.getProperty( "custom.data.export", Boolean.class, Boolean.FALSE );
}