Spring中配置和读取多个Properties文件--转
public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, InitializingBean
Allows for making a properties file from a classpath location available as Properties instance in a bean factory. Can be used to populate any bean property of type Properties via a bean reference.
Supports loading from a properties file and/or setting local properties on this FactoryBean. The created Properties instance will be merged from loaded and local values. If neither a location nor local properties are set, an exception will be thrown on initialization.
Can create a singleton or a new object on each request. Default is a singleton.
一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
- database.url=jdbc:mysql://localhost/smaple
 - database.driver=com.mysql.jdbc.Driver
 - database.user=root
 - database.password=123
 
2.消息服务配置文件demo-mq.properties:
- #congfig of ActiveMQ
 - mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
 - mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
 - mq.java.naming.security.principal=
 - mq.java.naming.security.credentials=
 - jms.MailNotifyQueue.consumer=5
 
3.远程调用的配置文件demo-remote.properties:
- remote.ip=localhost
 - remote.port=16800
 - remote.serviceName=test
 
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
- <?xml version="1.0" encoding="UTF-8"?>
 - <beans xmlns="http://www.springframework.org/schema/beans"
 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 - xsi:schemaLocation="
 - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 - <!-- 将多个配置文件读取到容器中,交给Spring管理 -->
 - <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 - <property name="locations">
 - <list>
 - <!-- 这里支持多种寻址方式:classpath和file -->
 - <value>classpath:/opt/demo/config/demo-db.properties</value>
 - <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
 - <value>file:/opt/demo/config/demo-mq.properties</value>
 - <value>file:/opt/demo/config/demo-remote.properties</value>
 - </list>
 - </property>
 - </bean>
 - <!-- 使用MQ中的配置 -->
 - <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
 - <property name="environment">
 - <props>
 - <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
 - <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
 - <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
 - <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
 - <prop key="userName">${mq.java.naming.security.principal}</prop>
 - <prop key="password">${mq.java.naming.security.credentials}</prop>
 - </props>
 - </property>
 - </bean>
 - </beans>
 
我们也可以将配置中的List抽取出来:
- <?xml version="1.0" encoding="UTF-8"?>
 - <beans xmlns="http://www.springframework.org/schema/beans"
 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 - xsi:schemaLocation="
 - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 - <!-- 将多个配置文件位置放到列表中 -->
 - <bean id="propertyResources" class="java.util.ArrayList">
 - <constructor-arg>
 - <list>
 - <!-- 这里支持多种寻址方式:classpath和file -->
 - <value>classpath:/opt/demo/config/demo-db.properties</value>
 - <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
 - <value>file:/opt/demo/config/demo-mq.properties</value>
 - <value>file:/opt/demo/config/demo-remote.properties</value>
 - </list>
 - </constructor-arg>
 - </bean>
 - <!-- 将配置文件读取到容器中,交给Spring管理 -->
 - <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 - <property name="locations" ref="propertyResources" />
 - </bean>
 - <!-- 使用MQ中的配置 -->
 - <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
 - <property name="environment">
 - <props>
 - <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
 - <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
 - <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
 - <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
 - <prop key="userName">${mq.java.naming.security.principal}</prop>
 - <prop key="password">${mq.java.naming.security.credentials}</prop>
 - </props>
 - </property>
 - </bean>
 - </beans>
 
二、整合多工程下的多个分散的Properties
应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。
配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
 - <beans xmlns="http://www.springframework.org/schema/beans"
 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 - xmlns:p="http://www.springframework.org/schema/p"
 - xsi:schemaLocation="
 - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 - <!-- 将DB属性配置文件位置放到列表中 -->
 - <bean id="dbResources" class="java.util.ArrayList">
 - <constructor-arg>
 - <list>
 - <value>file:/opt/demo/config/demo-db.properties</value>
 - </list>
 - </constructor-arg>
 - </bean>
 - <!-- 将MQ属性配置文件位置放到列表中 -->
 - <bean id="mqResources" class="java.util.ArrayList">
 - <constructor-arg>
 - <list>
 - <value>file:/opt/demo/config/demo-mq.properties</value>
 - </list>
 - </constructor-arg>
 - </bean>
 - <!-- 用Spring加载和管理DB属性配置文件 -->
 - <bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 - <property name="order" value="1" />
 - <property name="ignoreUnresolvablePlaceholders" value="true" />
 - <property name="locations" ref="dbResources" />
 - </bean>
 - <!-- 用Spring加载和管理MQ属性配置文件 -->
 - <bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 - <property name="order" value="2" />
 - <property name="ignoreUnresolvablePlaceholders" value="true" />
 - <property name="locations" ref="mqResources" />
 - </bean>
 - <!-- 使用DB中的配置属性 -->
 - <bean id="rmsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
 - p:driverClassName="${demo.db.driver}" p:url="${demo.db.url}" p:username="${demo.db.username}"
 - p:password="${demo.db.password}" pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"
 - p:poolPreparedStatements="true" p:defaultAutoCommit="false">
 - </bean>
 - <!-- 使用MQ中的配置 -->
 - <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
 - <property name="environment">
 - <props>
 - <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
 - <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
 - <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
 - <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
 - <prop key="userName">${mq.java.naming.security.principal}</prop>
 - <prop key="password">${mq.java.naming.security.credentials}</prop>
 - </props>
 - </property>
 - </bean>
 - </beans>
 
注意:其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。这里一定需要按照这种方式设置 这两个参数。
三、Bean中直接注入Properties配置文件中的值
应用场景:Bean中需要直接注入Properties配置文件中的值 。例如下面的代码中需要获取上述demo-remote.properties中的值:
- public class Client() {
 - private String ip;
 - private String port;
 - private String service;
 - }
 
配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
 - <beans xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"
 - xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
 - xmlns:util="<a href="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</a>"
 - xsi:schemaLocation="
 - <a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a>
 - <a href="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</a> <a href="http://www.springframework.org/schema/util/spring-util-3.0.xsd">http://www.springframework.org/schema/util/spring-util-3.0.xsd</a>">
 - <!-- 这种加载方式可以在代码中通过@Value注解进行注入,
 - 可以将配置整体赋给Properties类型的类变量,也可以取出其中的一项赋值给String类型的类变量 -->
 - <!-- <util:properties/> 标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签 -->
 - <util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" />
 - <!-- <util:properties/> 标签的实现类是PropertiesFactoryBean,
 - 直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的 -->
 - <bean id="settings"
 - class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 - <property name="locations">
 - <list>
 - <value>file:/opt/rms/config/rms-mq.properties</value>
 - <value>file:/opt/rms/config/rms-env.properties</value>
 - </list>
 - </property>
 - </bean>
 - </beans>
 
Client类中使用Annotation如下:
- import org.springframework.beans.factory.annotation.Value;
 - public class Client() {
 - @Value("#{remoteSettings['remote.ip']}")
 - private String ip;
 - @Value("#{remoteSettings['remote.port']}")
 - private String port;
 - @Value("#{remoteSettings['remote.serviceName']}")
 - private String service;
 - }
 
四、Bean中存在Properties类型的类变量
应用场景:当Bean中存在Properties类型的类变量需要以注入的方式初始化
1. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下
- import org.springframework.beans.factory.annotation.Value;
 - import org.springframework.beans.factory.annotation.Autowired;
 - public class Client() {
 - @Value("#{remoteSettings}")
 - private Properties remoteSettings;
 - }
 
2. 配置方式:也可以使用xml中声明Bean并且注入
- <?xml version="1.0" encoding="UTF-8"?>
 - <beans xmlns="http://www.springframework.org/schema/beans"
 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 - xsi:schemaLocation="
 - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 - <!-- 可以使用如下的方式声明Properties类型的FactoryBean来加载配置文件,这种方式就只能当做Properties属性注入,而不能获其中具体的值 -->
 - <bean id="remoteConfigs" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 - <property name="locations">
 - <list>
 - <value>file:/opt/demo/config/demo-remote.properties</value>
 - </list>
 - </property>
 - </bean>
 - <!-- 远端调用客户端类 -->
 - <bean id="client" class="com.demo.remote.Client">
 - <property name="properties" ref="remoteConfigs" />
 - </bean>
 - </beans>
 
代码如下:
- import org.springframework.beans.factory.annotation.Autowired;
 - public class Client() {
 - //@Autowired也可以使用
 - private Properties remoteSettings;
 - //getter setter
 - }
 
上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。
原文:http://kingxss.iteye.com/blog/1880681
补充:
The Spring Framework PropertiesFactoryBeanTests.java source code

/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.beans.factory.config; import java.util.Properties; import junit.framework.TestCase; import org.springframework.core.JdkVersion;
import org.springframework.core.io.ClassPathResource; /**
* @author Juergen Hoeller
* @since 01.11.2003
*/
public class PropertiesFactoryBeanTests extends TestCase { public void testWithPropertiesFile() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
} public void testWithPropertiesXmlFile() throws Exception {
// ignore for JDK < 1.5
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
return;
} PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test-properties.xml"));
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
} public void testWithLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("value2", props.getProperty("key2"));
} public void testWithPropertiesFileAndLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
} public void testWithPropertiesFileAndMultipleLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); Properties props1 = new Properties();
props1.setProperty("key2", "value2");
props1.setProperty("tb.array[0].age", "0"); Properties props2 = new Properties();
props2.setProperty("spring", "framework");
props2.setProperty("Don", "Mattingly"); Properties props3 = new Properties();
props3.setProperty("spider", "man");
props3.setProperty("bat", "man"); pfb.setPropertiesArray(new Properties[] {props1, props2, props3});
pfb.afterPropertiesSet(); Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
assertEquals("framework", props.getProperty("spring"));
assertEquals("Mattingly", props.getProperty("Don"));
assertEquals("man", props.getProperty("spider"));
assertEquals("man", props.getProperty("bat"));
} public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0");
pfb.setProperties(localProps);
pfb.setLocalOverride(true);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("0", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
} public void testWithPrototype() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setSingleton(false);
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
Properties newProps = (Properties) pfb.getObject();
assertTrue(props != newProps);
assertEquals("99", newProps.getProperty("tb.array[0].age"));
assertEquals("value2", newProps.getProperty("key2"));
} }

Spring中配置和读取多个Properties文件--转的更多相关文章
- Spring中配置和读取多个Properties文件
		
一个系统中通常会存在如下一些以Properties形式存在的配置文件 1.数据库配置文件demo-db.properties: database.url=jdbc:mysql://localhost/ ...
 - 在Spring中配置jdbc为什么不能用${username}问题
		
楼主在spring中配置jdbc时,引用的是dbcp.jar包,在dataSource.properties配置文件中,有mysql用户名,楼主自然的选择了使用username,密码是root, 然后 ...
 - Spring Boot配置,读取配置文件
		
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
 - spring中配置监听队列的MQ
		
一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置. <bean id="axx" ...
 - spring中配置数据源
		
spring中配置数据源的几种常见方式: #mysql 数据库配置(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.u ...
 - 在Spring中配置SQL server 2000
		
前言 Lz主要目的是在Spring中配置SQL server 2000数据库,但实现目的的过程中参差着许多SQL server 2000的知识,也包罗在本文记载下来!(Lz为什么要去搞sql serv ...
 - spring 中配置sessionFactory及用法
		
spring 中配置sessionFactory及用法 方法一: 1.在Spring的applicationContext.xml中配置bean <!-- 启用注解注入 --> ...
 - java如何读取和遍历properties文件
		
在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,很多如系统配置信息,文件上传配置信息等等都是以这种方式进行保存.同时学会操作properties文件也是java基础. ...
 - Java中如何获取spring中配置的properties文件内容
		
有2种方式: 一. 1.通过spring配置properties文件 [java] <bean id="propertyConfigurer" class=&qu ...
 
随机推荐
- android 让一个Activity停留几秒后再跳转
			
有时候我们需要在某个 Activity 停留几秒种,然后再跳到下一个 Activity.那么这个怎么实现呢? 一个方法是可以使用 Thread 的 sleep 函数,这个我们在 用Handler实现线 ...
 - Linux FHS
			
学习linux有必要了解FHS的知识,FHS英文全称是Filesystem Hierarchy Standard.中文翻译就是文件系统层次结构标准.关于这个详细的介绍参考官方的文档和鸟哥linux书. ...
 - POJ2195 Going Home[费用流|二分图最大权匹配]
			
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
 - jQuery超链接提示,提示跟随鼠标动
			
功能:实现鼠标移动到一个超链接时,鼠标右下角产生一个提示,并跟谁鼠标移动,知道鼠标离开超链接. 效果: 源码: <!--本案例是鼠标放在超链接上时,鼠标旁边有提示这是个超练级,以及放在图片上时图 ...
 - Recover data from reference electrode via EEGLab 用EEGLab恢复参考电极数据
			
The data of scanning reference electrode will not show initially. Here is a summary of recovering it ...
 - iTextSharp生成pdf的一个简单例子
			
效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...
 - Ext小总结
			
titleCollapse:true,//高级搜索点击隐藏显示 添加按钮 >弹出页面 1.添加按钮 //核销 在控制层页面添加页面路径>添加操作按钮>window.allOrder ...
 - Apache Shiro系列一,概述 —— 初识
			
一.什么是Shiro Apache Shiro是一个强大.灵活.开源的安全框架,它支持用户认证.权限控制.企业会话管理以及加密等. Apache Shiro的第一个也是最重要的一个目标就是易于使用和理 ...
 - 浅谈 underscore 内部方法 group 的设计原理
			
前言 真是天一热什么事都不想干,这个月只产出了一篇文章,赶紧写一篇压压惊! 前文(https://github.com/hanzichi/underscore-analysis/issues/15)说 ...
 - AutoHotKey实现将站点添加到IE的Intranet本地站点
			
最近在内部推行CRM系统,其中的CPQ组件要求必须将站点加入到"本地Intranet”才可以正常使用,但是由于使用用户比较多(超过几千人),并且每个用户的计算机水平都不一样,所以让用户手工去 ...