24

Situation

I am injecting properties from .properties file into fields annotated with @Value. However this properties present sensitive credentials, so I remove them from repository. I still want that in case someone wants to run project and doesnt have .properties file with credentials that default values will be set to fields.

Problem

Even if I set default values to field itself I get exception when .properties file is not present:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'secret' in string value "${secret}"

Here is the annotated field:

 @Value("${secret}")
 private String ldapSecret = "secret";

I expected in this case just plain String "secret" would be set.

RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36
  • Possible duplicate of [How to correctly specify a default value in the Spring @Value annotation?](http://stackoverflow.com/questions/26916598/how-to-correctly-specify-a-default-value-in-the-spring-value-annotation) – Nicolas Labrot Apr 05 '17 at 20:06

5 Answers5

36

To answer your question exactly...

@Value("${secret:secret}")
private String ldapSecret;

And a few more variations are below for completeness of the examples...

Default a String to null:

@Value("${secret:#{null}}")
private String secret;

Default a number:

@Value("${someNumber:0}")
private int someNumber;
Bernie Lenz
  • 1,967
  • 23
  • 45
  • You could shed some light that Spring Expression Language can be used for solving this issue. For example the `@Value("${secret:#{null}}")`. – RenatoIvancic Apr 19 '17 at 07:54
  • 6
    This is a nice answer, but it doesn't solve the problem. It explains how the system is SUPPOSED to work, but as of October 2017, Spring 4.2.4, it doesn't actually work that way. If you specify a default value in the @value annotation the actual value from the properties file doesn't get used. If you remove the default value from the annotation, then the value from the properties file IS used -- unless it is missing in which case an exception is thrown trying to instantiate the Bean. – Dale Wilson Oct 02 '17 at 15:45
  • 2
    Spring does not see the value from the property file if there is a default value, as of Feb2020. There is no way to work with both default value and value from property file – Vinh Feb 14 '20 at 00:04
  • 2
    This way of defining defaults works only if we add 'value=' in the annotation. e.g. @Value(value="${secret:secret}") will use default if property is not found in the config file. – Kislay Verma Aug 26 '20 at 05:50
  • @KislayVerma - that doesn't work. – Jack BeNimble Nov 11 '22 at 22:13
  • What should be the behavior of this -> ```@Value("${secret: }")``` ? – dcm50 Mar 31 '23 at 13:30
6

Just use:

@Value("${secret:default-secret-value}")
private String ldapSecret;
fps
  • 33,623
  • 8
  • 55
  • 110
2
@Value and Property Examples
To set a default value for property placeholder :

${property:default value}
Few examples :

//@PropertySource("classpath:/config.properties}")
//@Configuration

@Value("${mongodb.url:127.0.0.1}")
private String mongodbUrl;

@Value("#{'${mongodb.url:172.0.0.1}'}")
private String mongodbUrl;

@Value("#{config['mongodb.url']?:'127.0.0.1'}")
private String mongodbUrl;
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
0
@value(key.name:xyz)

Spring will give the first priority to read the value from the property file, If that doesn't exist then it will takes the default value(here 'xyz' is the default value).

There should not be any space to the key(key.name) before and after.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
-6

Actually the default value will be ALWAYS used. To overcome this, I use a String value

@Value("${prop}")
String propValue;//if no prop defined, the propValue is set to the literal "${prop}"
....
if("${prop}".equals(propValue)) {
     propValue=defaultValue
}
Vinh
  • 58
  • 4