Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

java - 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. 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:

@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, if property 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 exportData will still be false whereas it should be true ideally.

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Basically if more than a single PropertyPlaceholderConfigurer is present in the ApplicationContext, only predefined defaults will be resolved and no overrides will take place. 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...