chr1s
chr1s

Reputation: 43

Spring Expression Evaluation (Annotation)

I have the following issue:

Using Spring, I would like to propagate the value referenced by the property 'password' to a class variable:

 @Value("${password}")
 private String password;

That works as long as spring can find the property called 'password'. Since it will not be defined for all different executions, I would like to be able to have a default value assigned when the passwort property is not available.

I found the following example:

 @Value("${size_count?:5}")
 private int count;

This works. But when I try the same for password (for type String), it will always evaluate to false and return the string 'x' although the password property has been defined.

 @Value("${password?:x}")
 private String password;

any ideas?

Cheers chris

Upvotes: 2

Views: 1171

Answers (2)

axtavt
axtavt

Reputation: 242686

?: is used in Spring Expression Language, i.e. #{...}.

In property placeholders (${...}) you need to use ::

@Value("${password:x}") 
private String password; 

Upvotes: 1

Bozho
Bozho

Reputation: 597106

Try (I'm guessing, not sure if it makes sense)

@Value("${password}?:'x'")

Upvotes: 1

Related Questions