3

We're implementing Spring Boot application (v2.2.5) and there is a configuration that use @Value annotation as below (org.springframework.beans.factory.annotation.Value).

@Value("${something.about:abcd}")
private String somethingAbout;

In the application.yml file, we have property like this.

something:
  about: real_value

In this @Value annotation, I intended to work that as below:

  1. Use the property value when it's defined in the application.yml file.
  2. Otherwise, use the default value(abcd)

However, it does not read and set the existing property value to the variable. The variable is set as "abcd".

@Value("${something.about}") // Works fine

@Value("${something.about:abcd}") // It uses 'abcd' even the property exist

Is there anything that I missed?

Hosang Jeon
  • 1,373
  • 1
  • 17
  • 37
  • "the defaultValue" is literally "defaultValue" or (the identifier) of some other property? – xerx593 Jan 06 '22 at 06:46
  • I don't understand what you are trying to say is it `null` or is it the value you set as a default? Is this a normal configuration class or are you using a properties class and mistakenly use `@Value`. – M. Deinum Jan 06 '22 at 06:49
  • 2
    I wonder whether you have configured more than one property placeholder. If so you might hit https://github.com/spring-projects/spring-framework/issues/14623. – hradecek Jan 06 '22 at 06:54
  • @xerx593 Sorry. I've updated the question to make it clear. the "defaultValue" was meant to the iterally "defaultValue" (changed to "abcd") – Hosang Jeon Jan 06 '22 at 06:57
  • ok, "abcd" sounds more "constant" :), but then still "The variable is set as null." confuses (as the whole problem...it should work..even in spring-boot:2.2x) – xerx593 Jan 06 '22 at 07:00
  • @xerx593 I'm very sorry. I've missed to update the `null` part. I've updated it to "abcd" also. What exactly I want to ask is "the default value(abcd) overwrites the property value even if it exist". – Hosang Jeon Jan 06 '22 at 07:06
  • 2
    Possibly answered here : https://stackoverflow.com/questions/28369582/spring-boot-spring-always-assigns-default-value-to-property-despite-of-it-bein – pm. Jan 06 '22 at 07:14
  • 1
    yeah, this issue is was opened in 2012 (way outdated for this spring-versions), *but* [latest comment was last year](https://github.com/spring-projects/spring-framework/issues/14623#issuecomment-887415779), and it had to do with `PropertySourcePlaceholderConfigurer`!! (you have such bean configured?) – xerx593 Jan 06 '22 at 07:17
  • 3
    @xerx593 M. Deinum user17820797 Thank you for the help. As you recommended, I've found the cause of the issue. When I remove a annotation usage (A Swagger related annotation like spring fox) it work fine. Thanks – Hosang Jeon Jan 06 '22 at 07:57
  • 2
    @HosangJeon consider adding the solution as an answer and accepting it. Thanks! – João Dias Jan 06 '22 at 11:31

1 Answers1

2

Based on the helpful comments on the question, I'll summarize the cause and solution here.

Cause

  • The @Value property overwritten by default value issue is occurred because of the multiple PropertyPlaceholderConfigurer behavior.

  • We had an old version of Springfox for using Swagger and there was a bean declaration for PropertyPlaceholderConfigurer. It caused multiple behaviour with the original PropertySourcePlaceholderConfigurer in the spring-context.

  • Eventually, the proper property value written in application.yml file was overwritten the default value because of the multiple behavior of PropertyPlaceholderConfigurer bean in Springfox old version.

  • You can see the details from here : https://github.com/springfox/springfox/issues/1621

Solution

  • The known issue of Springfox has been resolved since version 2.7.0.

  • I've resolved the issue by updating the Springfox version to 3.0.0 to remove the duplicate PropertyPlaceholderConfigurer declaration.

Hosang Jeon
  • 1,373
  • 1
  • 17
  • 37