57

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.

mjj1409
  • 3,075
  • 6
  • 28
  • 28

3 Answers3

86

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'}")
Pranjal
  • 796
  • 1
  • 8
  • 24
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • 18
    For me on Spring 4.2 that resulted in: `'http://myurl.com'` (single quotes part of the value). Simply removing the single quotes fixed it. If I would guess, it splits on the first colon and the first part is the variable; the remainder is the value. – Amr Mostafa Feb 03 '16 at 12:02
2

If you need to pass a list of Strings that contain colon with default value then do like:

@Value("${parameterName:}#{T(java.util.Arrays).asList(\"abc:1\",\"def:2\")}")

private List<String> parameters;
barbariania
  • 499
  • 8
  • 19
0

On Spring version 3.2 the default value works without quotes.

karel
  • 5,489
  • 46
  • 45
  • 50