为什么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 读取配置文件的优先级相关推荐

  1. Spring读取配置文件,获取bean的几种方式

    Spring读取配置文件,获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemX ...

  2. Spring 读取配置文件(二)

    Spring 读取配置文件并调用 bean package cn.com.test.receive;import org.springframework.beans.factory.annotatio ...

  3. 关于spring读取配置文件的两种方式

    很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...

  4. Spring读取配置文件的几种方法(从0开始)

    新建一个Maven空项目 新建配置文件 在src -> main下新建一个资源目录resources,然后在该资源目录下新建一个application.properties文件. #自定义内容 ...

  5. spring读取配置文件初始化容器操作总结

    Spring初始化容器.三种经常用到的实现: 一.ClassPathXmlApplicationContext:从类路径中加载. 二.FileSystemXmlApplicationContext:从 ...

  6. spring读取配置文件的几种方式

    场景 假如有以下属性文件dev.properties, 需要注入下面的tag tag=123 通过PropertyPlaceholderConfigurer <bean class=" ...

  7. 10分钟搞定 SpringBoot 如何优雅读取配置文件?

    本文以及收录自springboot-guide(不只是SpringBoot还有Spring重要知识点),地址:https://github.com/Snailclimb/springboot-guid ...

  8. java读取配置文件的几种方法

    在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法. 一.读取xml配置 ...

  9. java加载xml配置文件_java读取配置文件的几种方法

    原标题:java读取配置文件的几种方法 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring ...

最新文章

  1. 尤佳轩、何恺明等提出新型图表示法,新视角理解图结构如何影响预测性能
  2. php临时文件夹,php-fpm临时文件路径问题【Sytemd PrivateTmp】
  3. c++同一屏幕输出多种颜色字体
  4. php正则重复匹配,php – 用于匹配任何长度的所有重复子串的正则表达式
  5. Boost : to_string_stub用法的测试程序
  6. 六月计划#2B(6.10-6.16)
  7. 【指数机制代码实现】差分隐私代码实现系列(十)
  8. AI 时代下的海量业务智能监控实践
  9. 个性化推荐算法-协同过滤
  10. python代码大全-python贪吃蛇游戏代码
  11. excel亮灯怎么设置_EXCEL高手进,用EXCEL实现亮灯功能
  12. Devexpress 各版本中文语言包
  13. 有两个杯子,一个5升一个6升,很多水,请问如何取得3升水
  14. B站最近很火的damedane,unravel图片唱歌
  15. 【RocketMQ】Send [1] times, still failed以及No route info of this topic问题排查思路总结
  16. (HR面试)最常见的面试问题和技巧性答复
  17. 寻找250(非数组求法)
  18. 数据结构实验6_压缩矩阵的转置、普通转置、快速转置算法
  19. html中div动态边框,[实践总结]纯css实现动态边框
  20. 汇总现阶段reid方面一些SOTA的paper。

热门文章

  1. MySQL 错误对照表
  2. hibernate QBC检索方式查询
  3. formdata传递参数_前端利用formData格式进行数据上传,前端formData 传值 和 json传值的区别?...
  4. 【计算理论】图灵机 ( 非确定性图灵机 -> 确定性图灵机 | 模仿过程示例 | 算法的数学模型 )
  5. 【Android 应用开发】Google 官方 EasyPermissions 权限申请库 ( 权限申请原理对话框操作回调接口 | 永久拒绝权限后引导设用户置权限 )
  6. 【Netty】Netty 核心组件 ( ServerBootstrap | Bootstrap )
  7. 【设计模式】外观模式 ( 概念 | 适用场景 | 优缺点 | 代码示例 )
  8. POJ - 1509 Glass Beads
  9. JavaScript(循环)
  10. C++ 关于方法传值