spring 读取配置文件的优先级

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配置为

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">

		<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="order" value="1"/>
			<property name="ignoreUnresolvablePlaceholders" value="true"/>
			<property name="ignoreResourceNotFound" value="false"></property>
			<property name="locations">
				<list>
					<value>classpath:p1.properties</value>
					<value>classpath:p2.properties</value>
				</list>
			</property>
		</bean>
</beans>

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

转载于:https://my.oschina.net/dylw/blog/777391

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值