我写了一个测试类来读取定义的属性
@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
运行测试时,输出
testone,DEFAULT
它没有从属性中获取test.two。
如果我没有指定默认值
@Value("${test.two}")
private String test2;
输出是 testone,测试
有人可以帮忙吗?
答案 0 :(得分:0)
我担心这个问题:“两个属性占位符和一个默认值”不容易解决,因为看起来默认值已经被第一个property-placeholder
“触发”,所以第二个没有“需要”再次“设置”此属性。
一个workarround只有一个property-placeholder
有两个配置文件和高位顺序:
<context:property-placeholder
locations="classpath*:/config/testone.properties,
classpath*:/config/testtwo.properties"
ignore-unresolvable="true" order="0" />
(注意:locations
属性而不是location
)