spring 加载配置文件的相关配置总结
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 加载配置文件的相关配置总结的更多相关文章
- spring加载配置文件
spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...
- Spring 加载配置文件的方式
我们常用的加载context文件的方法有如下三个: 1.FileSystemXmlApplicationContext 这个方法是从文件绝对路径加载配置文件,例如: ApplicationContex ...
- Webpack 2 视频教程 011 - Webpack2 中加载 CSS 的相关配置与实战
原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...
- Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)
一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplica ...
- SpringBoot加载配置文件(@PropertySource@importSource@Value)
情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...
- spring加载多个配置文件如何配置
为应用指定多个配置文件: 多个配置文件的关系: 并列 包含 并列关系 即有多个配置文件,需要同时加载这多个配置文件: 可以使用可变参数,数组和统配符进行加载: 可变参数 String config1 ...
- spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...
- 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 ...
- spring加载多个配置文件
首先我们都知道要使用spring,则需要在web.xml中增加如下代码: web.xml: 1:<listener><listener-class>org.springfram ...
随机推荐
- ReentrantLock(重入锁)以及公平性
ReentrantLock(重入锁)以及公平性 标签(空格分隔): java NIO 如果在绝对时间上,先对锁进行获取的请求一定被先满足,那么这个锁是公平的,反之,是不公平的,也就是说等待时间最长的线 ...
- Oracle EBS-SQL (SYS-18):检查系统安装的各个表是否打开(PJM%).sql
select status, trigger_name from all_triggers where trigger_name like 'PJM%'; ALTER TRIGGER PJM_ORG_ ...
- C#的线程池的那些事
最近在做站时发现,线程池的问题很棘手,所以总结了一篇关于线程池的文章,原文地址:http://www.shuonar.com/blog/ac16496b-87ec-4790-a9ea-d69bbffa ...
- 十九、Android Activity初探
原文:十九.Android Activity初探 Activity是一个应用中的组件,它为用户提供一个可视的界面,方便用户操作,比如说拔打电话.照相.发邮件或者是浏览地图等.每个activity会提供 ...
- js类方法,对象方法,原型的理解(转)
function People(name) { this.name=name; //对象方法 this.Introduce=function(){ alert("My name is &qu ...
- cocos2d-x3.6 连连看随机地图实现
我的博客:http://blog.csdn.net/dawn_moon 这一节来讲地图初始化实现. 连连看地图初始化有非常多实现方式,大概会有下面几种: 每一格的位置随机取图片放上去 随机取图片放到随 ...
- RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍
RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍 B/S结构(Browser/Server,浏览器/服务器模式),是WEB兴起后的一种网络结构模式,WEB浏览器是客户 ...
- CentOS 6.2图形界面自定义开启终端快捷键
菜单: System->Preferences->Keyboard Shortcuts 在Desktop分类下找到"Run a terminal" 点击Run a te ...
- SQL server 2008无法连接Local服务器的解决办法
SQL的有些服务总是在关机的时候自动关闭,然后你可以打开 控制面板——管理工具——服务,然后找到SQL相关的服务,右键——属性,里面有设置,可以设成自动启动
- rac安装中遇到的问题
ssh 建立面密码登陆时成功,但测试时却不成功,原因在于访问远端的文件时权限不够造成的: grid文件夹:755 grid账户下的.ssh文件夹:700 建立公共ip时需要设定域名:192.168.1 ...