java - Spring @Value annotated method, use default value when properties not available -


situation

i injecting properties .properties file fields annotated @value. properties present sensitive credentials, remove them repository. still want in case wants run project , doesnt have .properties file credentials default values set fields.

problem

even if set default values field exception when .properties file not present:

org.springframework.beans.factory.beancreationexception: error creating bean name 'xxx': injection of autowired dependencies failed; nested exception java.lang.illegalargumentexception: not resolve placeholder 'secret' in string value "${secret}" 

here annotated field:

 @value("${secret}")  private string ldapsecret = "secret"; 

i expected in case plain string "secret" set.

to answer question exactly...

@value("${secret:secret}") private string ldapsecret; 

and few more variations below completeness of examples...

default string null:

@value("${secret:#{null}}") private string secret; 

default number:

@value("${somenumber:0}") private int somenumber; 

Comments