赞
踩
spring 读取配置文件的优先级
1 结论
在spring加载properties配置文件的过程中,会根据key出现覆盖现象,后加载的覆盖前面的。
加载顺序
key
value
最终输出
p1
testConver
没有覆盖,为p1
覆盖,为p2属性
p1
test
测试区
测试区
p2
testConver
覆盖,为p2属性
覆盖,为p2属性
p2
test
开发区
开发区
2 上干活
2.1 java 代码
单元测试类为:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:/spring-resources.xml")
public class SpringPropertiesTest {
@Value("${testConver}")
private String testConver;
@Value("${dev}")
private String dev;
@Value("${test}")
private String test;
@Test
public void showPropValue() {
System.out.println(testConver);
System.out.println(dev);
System.out.println(test);
}
}
2.2 spring配置
spring-resources.xml配置为
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
classpath:p1.properties
classpath:p2.properties
2.3 配置文件
p1.properties:
testConver=没有覆盖,为p1
test=测试区
p2.properties:
testConver=覆盖,为p2属性
dev=开发区
2.4 控制台输出结果
控制台输出内容为:
十月 28, 2016 3:24:18 下午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.dxhy.spring.SpringPropertiesTest]: using defaults.
十月 28, 2016 3:24:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-resources.xml]
十月 28, 2016 3:24:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@2723a510: startup date [Fri Oct 28 15:24:19 CST 2016]; root of context hierarchy
十月 28, 2016 3:24:19 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [p1.properties]
十月 28, 2016 3:24:19 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [p2.properties]
十月 28, 2016 3:24:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@690a614: defining beans [propertyConfigurer,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
覆盖,为p2属性
开发区
测试区
十月 28, 2016 3:24:19 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@2723a510: startup date [Fri Oct 28 15:24:19 CST 2016]; root of context hierarchy
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。