java 具有多个位置的 Spring 属性占位符配置器中的属性解析顺序是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14192373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is property resolution order in Spring property placeholder configurer with multiple locations?
提问by Vladimir
Lets say I have a configuration:
假设我有一个配置:
<bean id="batchJobProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>first.properties</value>
<value>second.properties</value>
</list>
</property>
</bean>
first.properties has property "my.url=first.url" second.properties has property "my.url=second.url"
first.properties 具有属性“my.url=first.url” second.properties 具有属性“my.url=second.url”
So which value will be injected to the "myUrl" bean? Is there is any defined order of properties resolution?
那么哪个值会被注入到“myUrl”bean 中呢?是否有任何定义的属性解析顺序?
回答by Mark
The javadoc for PropertiesLoaderSupport.setLocationstates
PropertiesLoaderSupport.setLocation的 javadoc状态
Set locations of properties files to be loaded.
Can point to classic properties files or to XML files that follow JDK 1.5's properties XML format.
Note: Properties defined in later files will override properties defined earlier files, in case of overlapping keys. Hence, make sure that the most specific files are the last ones in the given list of locations.
设置要加载的属性文件的位置。
可以指向经典属性文件或遵循 JDK 1.5 的属性 XML 格式的 XML 文件。
注意:在重叠键的情况下,稍后文件中定义的属性将覆盖先前文件中定义的属性。因此,请确保最具体的文件是给定位置列表中的最后一个。
So the value of my.url in second.properties will override the value of my.url in first.properties.
因此,second.properties 中 my.url 的值将覆盖 first.properties 中 my.url 的值。
回答by Evgeniy Dorofeev
The last one wins.
最后一位获胜。
Assuming we have props1.properties as
假设我们有 props1.properties 作为
prop1=val1
and props2.properties
和 props2.properties
prop1=val2
and context.xml
和上下文.xml
<context:annotation-config />
<bean id="batchJobProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/props1.properties</value>
<value>/props2.properties</value>
</list>
</property>
</bean>
<bean class="test.Test1" />
then
然后
public class Test1 {
@Value("${prop1}")
String prop1;
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/test1.xml");
System.out.println(ctx.getBean(Test1.class).prop1);
}
}
prints
印刷
val2
值2
and if we change context as
如果我们将上下文更改为
<list>
<value>/props2.properties</value>
<value>/props1.properties</value>
</list>
the same test prints
相同的测试打印
val1