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. J2SE知识点摘记(十)

    1.        多线程的同步 2.        同步代码块 即程序中不能有多个线程同时在这两句代码之间执行.(必须两句代码执行完,其他线程才可以公用执行) 同步代码块定义语法如下 ... syn ...

  2. arcEngine添加标注(上)

    arcEngine添加标注有3个技术点:1,获得图层的属性字段:2,初始化符号显示控件:3,添加标注. 获得图层的属性字段:   //每个图层都是一个表的图形化显示,ILayer跟ITale可以互相转 ...

  3. 奇怪的问题:android:focusable和android:clickable造成ListView的点击不了

    今天花了我很长时间,才解决一个很奇怪的问题,就是在ListView的点击反应不了的问题…… 在ListView中,如果其中一个元素设置为android:focusable="true&quo ...

  4. 解决ArcSDE图层名被占用的问题

    点击文章查看 当你发现上面那篇文章时你已经接近成功了,但还差最后一步! <!- -delete   from sde.GDB_OBJECTCLASSES where NAME = upper(' ...

  5. loading android

    drawal/loading.xml <?xml version="1.0" encoding="utf-8"?><animated-rota ...

  6. android部分控件应用解析

    java中的接口回调机制图解 1. Adapter 接口概述   Adapter是一个顶层列表视图和底层数据的桥梁,通过adapter可以获取列表视图中所体现的数据条目,并且通过adapter可以为数 ...

  7. 存储过程中调用EXECUTE IMMEDIATE的“权限不足”问题

    使用plsql 动态创建表时,用户需要具有create any table 权限 例如: create or replace procedure create_table_test is tmpstr ...

  8. 微信小程序环境搭建

    第一步  IDE下载 Win版链接: https://pan.baidu.com/s/1gfAy18n 第二步 破解 最新下载链接: https://pan.baidu.com/s/1gfAy18n ...

  9. asp.net word内容读取到页面

    1.添加Microsoft.Vbe.Interop.dll引用. 2.以下方法可以简单的读取到word文档文字内容,不包括图片.格式等. private string ReadWordFile(str ...

  10. Hibernate学习之关系映射(转)

    一.一对多 "一对多"是最普遍的映射关系,简单来讲就如消费者与订单的关系.一对多:从消费者角的度来说一个消费者可以有多个订单,即为一对多.多对一:从订单的角度来说多个订单可以对应一 ...