Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Value escape colon(:) in default value

I have the following property annotated with @Value. I have a default value defined using the default separator of ':"

@Value("${prop.url:http://myurl.com}") 

Is there a way to escape the ':' in http://myurl.com or do I have to define a different separator value in my configuration.

like image 585
mjj1409 Avatar asked Jul 29 '15 21:07

mjj1409


People also ask

How do you escape colon (:) in properties file?

Make a StringWriter, write your properties file into it, go through the buffer line-by-line looking for "\:" , and replacing it by a single ":" .

How do I set default value in spring?

To set a default value for primitive types such as boolean and int, we use the literal value: @Value("${some. key:true}") private boolean booleanWithDefaultValue; @Value("${some.

What does @value mean in spring boot?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation.


1 Answers

Update: For spring 4.2 and higher, no single quotes are needed. Spring will see the first colon as special, and use all the rest as a single string value.

For spring 4.2 and higher,

@Value("${prop.url:http://myurl.com}") 

For the previous versions, I believe single quotes will do the trick:

@Value("${prop.url:'http://myurl.com'}") 
like image 66
Chris Thompson Avatar answered Sep 19 '22 17:09

Chris Thompson