PropertyPlaceholderConfigurer
      注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉。
Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。

一:配置单个Properties文件

 

<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config/sysconfig/sysconfig.properties</value>
</property>
</bean>
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/sysconfig/sysconfig.properties"/>
</bean>

二:配置多个Properties文件

<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/dbconfig/database.properties</value>
<value>classpath:config/sysconfig/sysconfig.properties</value>
<value>file:/opt/demo/config/demo-mq.properties</value>
</list>
</property>
</bean>

三:简化操作
为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

<context:property-placeholder location="classpath:jdbc.properties"/>

四:参考用例

1.

1. applicationContext.xml
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations" >
<list>
<value>classpath:config/dbconfig/database.properties</value>
<value>classpath:config/sysconfig/sysconfig.properties</value>
</list>
</property>
</bean>
// 其中classpath是引用src目录下的文件写法 <import resource="classpath:config/spring/spring-data.xml" /> spring-data.xml
<context:property-placeholder properties-ref="deployProperties" />

2.

applicationContext.xml
<context:property-placeholder location="classpath:config/*config/*.properties"/>

3.

applicationContext.xml
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/dbconfig/database.properties</value>
<value>classpath:config/sysconfig/sysconfig.properties</value>
<value>file:/opt/demo/config/demo-mq.properties</value>
</list>
</property>
</bean>

4.

applicationContext.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location" value="classpath:config/dbconfig/database.properties"/>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location" value="classpath:config/sysconfig/sysconfig.properties"/>
</bean>

其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true

5.

applicationContext.xml
<bean id="dbResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>classpath:config/dbconfig/database.properties</value>
<value>classpath:config/sysconfig/sysconfig.properties</value>
</list>
</constructor-arg>
</bean> <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="dbResources"> </property>
</bean>

6.

applicationContext.xml
<bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations" >
<list>
<value>classpath:config/dbconfig/database.properties</value>
<value>classpath:config/sysconfig/sysconfig.properties</value>
</list>
</property>
</bean>
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="configproperties">
</property>
</bean>

7.context 标签和PropertyPlaceholderConfigurer 混合在一起要加ignoreUnresolvablePlaceholders

<context:property-placeholder location="classpath:config/*config/database.properties"/>

 <bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location" value="classpath:config/sysconfig/sysconfig.properties"/>
</bean>

纯属个人总结和网络搜索的资料

spring 加载配置文件的相关配置总结的更多相关文章

  1. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  2. Spring 加载配置文件的方式

    我们常用的加载context文件的方法有如下三个: 1.FileSystemXmlApplicationContext 这个方法是从文件绝对路径加载配置文件,例如: ApplicationContex ...

  3. Webpack 2 视频教程 011 - Webpack2 中加载 CSS 的相关配置与实战

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  4. Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

    一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplica ...

  5. SpringBoot加载配置文件(@PropertySource@importSource@Value)

    情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...

  6. spring加载多个配置文件如何配置

    为应用指定多个配置文件: 多个配置文件的关系: 并列 包含 并列关系 即有多个配置文件,需要同时加载这多个配置文件: 可以使用可变参数,数组和统配符进行加载: 可变参数 String config1 ...

  7. spring加载jar包中多个配置文件(转)

    转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...

  8. 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)

    1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置  1.命令行参数 所有的配置都可以在命令行上进行指定 ...

  9. spring加载多个配置文件

    首先我们都知道要使用spring,则需要在web.xml中增加如下代码: web.xml: 1:<listener><listener-class>org.springfram ...

随机推荐

  1. RMA编码为必输

    应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name RCV_RCVTXERE 表单名 Form Name RCVTXERE 说明 Descr ...

  2. Ubuntu eclipse :An error has occurred. See the log file

    安装eclipse: sudo apt-get install eclipse-platform 调整java: sudo update-alternatives --config java 启动: ...

  3. Bash debug

    Debugging bash scripts Bash can help us to find problems in bash scripts in some ways. You don't exp ...

  4. 深入Blocks分析

    1.简介 从iOS4开始,苹果引入了这个C语言的扩充功能"Blocks",在一些特定的场景下也是一把利刃.我前面一篇博客中初步介绍了Blocks这个东西,主要是语法的介绍(< ...

  5. ntpd和ntpdate的区别

    之前配置ntpd的时候搜到一句话,印象很深刻,也觉得很有标题党的效果,就借鉴为标题了:“我认为有几种人是必须不招聘/裁掉的: 1 用ntpdate代替ntpd的人”但具体原因不太懂,总觉得还是用ntp ...

  6. 面向对象程序设计-C++_课时24多态的实现

    所有带virtual的类的对象,里面最上面有一个隐藏的指针vptr,指向一张表vtable #include <iostream> using namespace std; class A ...

  7. 2014 Web开发趋势

    本文翻译自:http://www.pixelstech.net/article/1401629232-Web-design-trends-for-2014 如今,已然到了Web横行的时代.越来越多的资 ...

  8. CouchDB简单应用

    CouchDB是众多称作NoSQL解决方案中的一员.与众不同的是,CouchDB是一个面向文档的数据库,在它里面所有文档域(Field)都是以键值对的形式存储的.域(Field)可以是一个简单的键值对 ...

  9. HTML系列(八):表格

    一.基本表格: 表格标记<table>,行标记<tr>,单元格标记<td> 基本语法: <table> <tr> <td>单元格 ...

  10. VB.NET中vbcr 是回车、vbcrlf 是回车和换行的结合、vblf 是换行

    cr 是回车,是到本行的头部 lf 是换行,是到下一行 crlf 是到下一行的头部 vbcrlf=vbcr   &   vblf Windows     一般使用vbcrlf换行 Unix   ...