简体   繁体   English

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

[英]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: 您可以执行以下任一操作来克服此问题:

  1. Use custom valueSeparator in your configurer 在配置器中使用自定义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 使用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 . 我已经在此问题上做了一些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.

相关问题 Spring boot - 使用配置文件时未读取默认属性文件 - Spring boot - Default properties file not being read when using profile spring 引导应用程序属性中的默认字段值 - Default field value in spring boot application properties spring 引导默认属性值中的大括号 - curly brace in spring boot default property value Spring Boot不会从application.properties文件中读取默认值 - Spring Boot doesn't read the default value from the application.properties file Spring Boot中的外部YAML属性文件和常规属性文件 - External YAML property file and normal properties file in Spring Boot Spring-boot:将默认值设置为可配置属性 - Spring-boot: set default value to configurable properties 在Spring Boot中未从外部化属性文件中读取自定义值 - Custom values not being read from externalized properties file in Spring Boot Spring Boot应用程序的@Value属性始终返回null - Spring boot application @Value property always returns null 在初始化时加载 Spring 引导属性,并根据属性文件中的值尊重所有和控制 @Aspect - Spring boot properties to be loaded at initialization and respect all and control @Aspect based on the value from property file 如何从 spring boot 项目的 application.properties 文件中获取属性值? - How to get property value from application.properties file on spring boot project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM