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 ...
随机推荐
- Microsoft web application stress tool 测试
一.准备工作 为了测试数据的准备性,首先需要删除缓存和Cookies等临时文件.启动IE后打开“工具”菜单下的“Internet”选项命令,在打开的“Internet选项”窗口的“常规”选项卡中,单击 ...
- CentOS7 vs centos6
The CentOS Project has announced general availability of CentOS-7, the first release of the free Lin ...
- HDU 5723 Abandoned country(最小生成树 + 树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...
- 如何在XML 加入特殊字符内容 如< >
XML 文件本身包含了一些预定义的保留字符 如< 标记元素的开始符号等 如果要在属性或者元素的值里面包含类似的这些特殊字符 应该如何处理呢 ? 这时候要用到 <![CDATA[] 这个标 ...
- Linux系统CentOS6.2版本号下安装JDK7具体过程
前言: java 是一种能够撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE( ...
- C#生成PDF
参考书籍 http://wenku.baidu.com/link?url=N2jjf_JNIuKUxkODxePvNTlZNyFQ6qsx_TPKenDqhL9ShKojw95gs38fHihXg2e ...
- ASP.NET导出EXCEl方法使用EXCEl对象
导出功能必须使用 office中EXCEl对象,整个操作如同在操作EXCEl一样,建立EXCEl应用----建立工作簿---建立sheet表单页, 代码实现过程中,如果想对单元格实现一些操作,或者汇 ...
- JavaScript之<script>标签简介
向html页面中插入JavaScrpt的主要方法,就是使用<script>元素,下面是Html 4.01为<script>定义的6个属性. 1.async:可选表示应该立即下载 ...
- JavaScript之共享onload
我们知道,当我们将JS代码脚本放到<head></head>标签之间时,这是的js代码加载要先于DOM加载,而我们往往会在JS代码脚本中写一些获取DOM元素的代码,而此时的DO ...
- Html5 自定义数据属性
html5 可以为元素添加自定义属性,但是要添加前缀data-.(下面这个例子中的自定义属性的命名,其实是不规范的,不应该包含大写字符,例如:data-myName 应改命名为:data-myname ...