0

I wrote a test class to read properties defined

    @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:config/TestOne.xml","classpath*:config/TestTwo.xml"
})
public class PropertyTest {
        @Value("${test.one:DEFAULT}")
        private String test;
        @Value("${test.two:DEFAULT}")
        private String test2;

        @Test
        public void TestProperty(){

                System.out.println(test + "," + test2);

        }

}

TestOne.xml

  <context:property-placeholder
                location="classpath*:/config/testone.properties"                
                ignore-unresolvable="true" order="1" />  

TestTwo.xml

<context:property-placeholder
                location="classpath*:/config/testtwo.properties"               
                ignore-unresolvable="true" order="2" />

testone.properties

test.one=testone

testtwo.properties

test.one=testone

test.two=test

While running the Test, output is

testone,DEFAULT

it is not fetching test.two from property.

If I am not specifying the default value

@Value("${test.two}")
        private String test2;

Output is testone,test

Can anybody help?

Jithin
  • 31
  • 3

1 Answers1

0

I fear that this problem: "two property placeholder and a default value" is not easy to solve, because it looks like the default value is already "triggerd" by the first property-placeholder so the second one does not "need" to set this property "again".

A workarround is to have only one property-placeholder with two config files and the highes order:

<context:property-placeholder
            locations="classpath*:/config/testone.properties,
                       classpath*:/config/testtwo.properties"                
            ignore-unresolvable="true" order="0" />  

(attention: locations property instead of location)

Ralph
  • 118,862
  • 56
  • 287
  • 383