欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

spring 读取配置文件的优先级

发布时间:2025/6/15 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 spring 读取配置文件的优先级 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

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;@Testpublic 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

总结

以上是生活随笔为你收集整理的spring 读取配置文件的优先级的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。