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 ...
随机推荐
- android一分钟学会可视化操作数据库(无需ROOT)
我刚开始弄android数据库的时候,想查询一些数据,以验证程序逻辑,发现很多方案都需要ROOT. 即便有不需要ROOT的方案,命令行交互也比较麻烦. 今天跟大家分享一下这个点点鼠标就能实现的功能. ...
- 转: ES6异步编程:Thunk函数的含义与用法
转: ES6异步编程:Thunk函数的含义与用法 参数的求值策略 Thunk函数早在上个世纪60年代就诞生了. 那时,编程语言刚刚起步,计算机学家还在研究,编译器怎么写比较好.一个争论的焦点是&quo ...
- Android手机app启动的时候第一个Activity必须是MainActivity吗
原文:Android手机app启动的时候第一个Activity必须是MainActivity吗 Android手机APP启动的第一个Activity是可以自己设置的,不是必须的MainActivity ...
- 基于Java图片数据库Neo4j 3.0.0发布 全新的内部架构
基于Java图片数据库Neo4j 3.0.0发布 全新的内部架构 Neo4j 3.0.0 正式发布,这是 Neo4j 3.0 系列的第一个版本.此版本对内部架构进行了全新的设计;提供给开发者更强大的生 ...
- ZooKeeper源码阅读(二):客户端
源代码: http://svn.apache.org/repos/asf/zookeeper/trunk/ 导入eclipse: 在包含build.xml目录下执行ant eclipse将产生.cla ...
- CentOS 安装Node.js
先安装gcc-c++编译环境和openssl yum install gcc-c++ openssl-devel 然后 下载包并解压 cd /usr/local/src wget http://nod ...
- <转>机器学习笔记之奇异值分解的几何解释与简单应用
看到的一篇比较好的关于SVD几何解释与简单应用的文章,其实是有中文译本的,但是翻译的太烂,还不如直接看英文原文的.课本上学的往往是知其然不知其所以然,希望这篇文能为所有初学svd的童鞋提供些直观的认识 ...
- Ghost Button制作教程及设计趋势分析
概述:Ghost Button(虚拟按钮)是网页设计中一个非常实用的按钮样式,特别是图片背景中,有出色的效果.今天我们一起来研究Ghost Button的各种效果的制作方法,并对Ghost Butto ...
- English - every和each的用法和区别
两者都有“每个”的意思,但用法不同: (1)each具有名词和形容词的功能,every只有形容词的功能. (2)each指两个或两个以上的人或事物中的“每个”:every是指三个以上的人或事物的“全体 ...
- 以Android环境为例的多线程学习笔记(二)-----------------锁和条件机制
现在的绝大多数应用程序都是多线程的程序,而当有两个或两个以上的线程需要对同一数据进行存取时,就会出现条件竞争,也即 是这几个线程中都会有一段修改该数据状态的代码.但是如果这些线程的运行顺序推行不当的话 ...