[英]Spring Boot : Spring always assigns default value to property despite of it being present in .properties file
I am working with Spring boot 1.1.8 which uses Spring 4.0.7.我正在使用使用 Spring 4.0.7 的 Spring boot 1.1.8。 I am autowiring the properties in my classes with @Value annotation.
我正在使用 @Value 注释自动装配我的类中的属性。 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:
下面是示例:
@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.如果属性在属性文件中不存在,它应该为变量分配 false。 However, if property is present in the file, then also it assigns default value and ignores the properties value.
但是,如果文件中存在属性,那么它也会分配默认值并忽略属性值。 Eg if I have defined the property like the one mentioned above and application properties file has something like this
custom.data.export=true
then, the value of exportData
will still be false whereas it should be true ideally.例如,如果我像上面提到的那样定义了属性,并且应用程序属性文件有这样的内容
custom.data.export=true
那么, exportData
的值仍然是假的,而理想情况下应该是真。
Can anyone please guide me what I am doing wrong here?谁能指导我我在这里做错了什么?
Thanks谢谢
We were bitten by the following Spring bug with exactly the same symptom: 我们被以下具有相同症状的Spring bug所咬伤:
[SPR-9989] Using multiple PropertyPlaceholderConfigurer breaks @Value default value behavior [SPR-9989]使用多个PropertyPlaceholderConfigurer会破坏@Value默认值行为
Basically if more than a single PropertyPlaceholderConfigurer
is present in the ApplicationContext, only predefined defaults will be resolved and no overrides will take place. 基本上,如果ApplicationContext中存在多个
PropertyPlaceholderConfigurer
,则仅解析预定义的默认值,并且不会发生任何替代。 Setting a different ignoreUnresolvablePlaceholders
value had no impact on the matter, and both values (true/false) worked equally well in that regard once we removed the extra PropertyPlaceholderConfigurer
. 设置一个不同的
ignoreUnresolvablePlaceholders
值对此问题没有影响,并且一旦我们删除了额外的PropertyPlaceholderConfigurer
,这两个值(true / false)在这方面都可以很好地工作。
Looking into it, each of the defined PropertyPlaceholderConfigurer
internally resolved the properties as expected, but Spring couldn't figure out which of them to use in order to inject a value into the @Value
annotated fields/params. 仔细研究一下,每个定义的
PropertyPlaceholderConfigurer
内部都按预期解析了属性,但是Spring无法弄清楚要使用哪个属性将值注入@Value
注释字段/参数中。
You can do one of the following to overcome this: 您可以执行以下任一操作来克服此问题:
<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>
<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 . 我已经在此问题上做了一些RnD, 可在此处找到 。
As @Ophir Radnitz stated, this is a spring bug that happens when there is more than one PropertyPlaceholderConfigurer present in the ApplicationContext. 正如@Ophir Radnitz所说,这是一个Spring错误,当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 );
}
Happening this situation, depends on the type of the parameter.发生这种情况,取决于参数的类型。
when setting a default value for a String
parameter, your sample code as default value ( @Value("${custom.string:test}")
) works fine, for other types (like boolean
, in your question), the default value should be written in this way:为
String
参数设置默认值时,您的示例代码作为默认值( @Value("${custom.string:test}")
)工作正常,对于其他类型(如boolean
,在您的问题中),默认值应该这样写:
@Value("${custom.data.export:#{true}}")
private boolean exportData = true;
similarly, for Integers
:类似地,对于
Integers
:
@Value("${custom.integer:#{20}}")
good luck.祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.