2

When defaulting values in a Spring XML configuration file with the following snippet:

<util:properties id="defaultConfiguration">
    <prop key="test.value">${first.value:notFilledIn}</prop>
</util:properties>
<context:property-placeholder properties-ref="defaultConfiguration" order="605" ignore-unresolvable="true"/>

With the following properties being loaded:

first.value=first
second.value=second

The value always comes back as the "default" value, not the first key.

@Value("${test.value}")
private String theValue;
.....
System.out.println("theValue: " + theValue);

Output:

theValue: notFilledIn

If I change the prop value in the XML config to:

<prop key="test.value">${first.value}</prop>

The value comes back as expected:

theValue: first

Why is the default value always being pulled, when the "key" to the first value exists?

LetsBeFrank
  • 774
  • 11
  • 31
  • 1
    'With the following properties being loaded' How are you loading them? Also what version of spring are you using? The meaning of context:property-placeholder differs by version – roby Jun 10 '15 at 23:26
  • what happens if you dont use default value at all? Does it inject proper one or throws exception? Maybe property placeholder is loaded too late in context? – hi_my_name_is Jun 11 '15 at 06:35
  • 2
    I am using Spring 4.0.8. The last example above shows what happens with no default value. A single property lookup works just fine (without a default). – LetsBeFrank Jun 11 '15 at 14:29
  • 2
    Properties (first.value and last.value) are being loaded as such: This is loaded prior to the other items above. Again, works with either value standalone, just not when using the colon "default", as it only always takes the default one. – LetsBeFrank Jun 11 '15 at 14:32

1 Answers1

0

I had the same problem, I hope this helps anyone.

make sure you only have one

<context:property-placeholder>

in your code, otherwise they override each other and your keys will not be set

Erez
  • 42
  • 5