Spring PropertyPlaceholderConfigurer 自定义扩展
来源:互联网 发布:大话西游3手游下载mac 编辑:程序博客网 时间:2024/05/16 12:21
参考:http://seraph115.iteye.com/blog/435165
Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spring配置期间替换使用属性值。接下来让我们逐渐的深入其配置。
基本的使用方法是:
<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:/spring/include/dbQuery.properties</value> </property> </bean>
其中classpath是引用src目录下的文件写法。
当存在多个Properties文件时,配置就需使用locations了:(2)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/spring/include/jdbc-parms.properties</value> <value>classpath:/spring/include/base-config.properties</value> </list> </property> </bean>
接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下:(3)
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <value>classpath:/spring/include/dbQuery.properties</value> </list> </property> </bean> <bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="2" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <value>classpath:/spring/include/jdbc-parms.properties</value> <value>classpath:/spring/include/base-config.properties</value> </list> </property> </bean>
其中order属性代表其加载的顺序,如果没有设置就按照加载xml文件时的顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如果配置了多个PropertyPlaceholderConfigurer,则该属性必须设置且为true,否则propertyConfigurerForProject2的properties文件不会被加载.
至此你已经了解到了如何使用PropertyPlaceholderConfigurer,如何使用多个Properties文件,以及如何配置多个PropertyPlaceholderConfigurer来分解工程中分散的Properties文件。至于PropertyPlaceholderConfigurer还有更多的扩展应用,如属性文件加密解密等方法将在之后的博文中续写。
注意事项:
(1)如果上面的dbQuery.properties与jdbc-parms.properties文件中有相同的参数配置名称,dbQuery.properties中配置的参数值不会被后面的覆盖;
(2)如果jdbc-parms.properties,base-config.properties彼此有相同参数名配置,jdbc-parms.properties中的配置的值会被覆盖;
自定义扩展PropertyPlaceholderConfigurer实现
例如:配置文件的路径,需要动态确定的,就需要自己扩展PropertyPlaceholderConfigurer的实现,自己获取文件路径,load properties文件,然后将load后的properties加入PropertyPlaceholderConfigurer
package com.common.spring.ext;import java.util.Properties;import java.util.Set;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import com.common.exception.ApplicationException;import com.common.util.GlobalProperties;import com.common.util.PropertiesUtil;public class GollfPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { public void setGollfPropFiles(Set<String> gollfPropFiles) { String propPath = GlobalProperties.getProperty(GlobalProperties.PROPERTIES_FOLDER_PATH); //通过其他配置获取路径 String fileSeparator = System.getProperty("file.separator"); Properties properties = new Properties(); for (String gollfPropFile : gollfPropFiles) { String nodeName = System.getProperty("weblogic.Name"); gollfPropFile = gollfPropFile.replaceAll("\\[NODE_NAME\\]", nodeName); //NODE_NAME 是根据不同weblogic server确定 String file = propPath + fileSeparator + gollfPropFile; try { logger.info("Loading properites file from " + file); Properties prop = PropertiesUtil.loadProperties(file); //返回properties文件 logger.debug("Properties -> " + prop); if(prop != null) { properties.putAll(prop); } } catch (Exception e) { logger.fatal(new ApplicationException("Properties file " + gollfPropFile + " cannot be found. All related functionalities may be unavailable", e, true)); } } this.setProperties(properties); //关键方法,调用的PropertyPlaceholderConfigurer中的方法, //通过这个方法将自定义加载的properties文件加入spring中 }}
xml配置
<bean id="auditJmsProperties" class="com.common.spring.ext.GollfPropertyPlaceholderConfigurer"> <property name="gollfPropFiles"> <set> <value>[NODE_NAME]_jms.properties</value> </set> </property> </bean>
PropertyPlaceholderConfigurer中加载properties文件时,实际调用的:org.springframework.core.io.support.PropertiesLoaderSupport中的mergeProperties
Spring源码
protected Properties mergeProperties() throws IOException { Properties result = new Properties(); if (this.localOverride) { // Load properties from file upfront, to let local properties override. loadProperties(result); } if (this.localProperties != null) { for (Properties localProp : this.localProperties) { //将用户自定义加载的属性值,与spring加载的合并 CollectionUtils.mergePropertiesIntoMap(localProp, result); } } if (!this.localOverride) { // Load properties from file afterwards, to let those properties override. loadProperties(result); } return result; }
将多个properties文件中的配置加载以后合并成一个Properties对象返回.
上面的this.setProperties(properties)方法,就是设置localProperties的引用,localProperties不为空的话,将用户自定义加载的properties属性合并到Spring加载的result Properties对象中
localOverride参数:为true的话,表示用户自定义加载的属性值覆盖spring系统加载的,如果同名的话.
自定义使用注意:用户自定义方法的调用务必在spring 初始化调用PropertyPlaceholderConfigurer的mergeProperties()方法之前调用,否则配置文件就没有合并.一般就set值的时候调用.
- Spring PropertyPlaceholderConfigurer 自定义扩展
- Spring PropertyPlaceholderConfigurer 自定义扩展
- Spring PropertyPlaceholderConfigurer 自定义扩展
- Spring容器扩展--PropertyPlaceholderConfigurer
- spring扩展:PropertyPlaceHolderConfigurer
- Spring自定义PropertyPlaceholderConfigurer
- spring boot 自定义PropertyPlaceholderConfigurer
- Spring自定义PropertyPlaceholderConfigurer
- spring PropertyPlaceholderConfigurer自定义属性
- Spring PropertyPlaceholderConfigurer 自定义扩展来解决多环境配置加载的问题
- Spring容器扩展点-PropertyPlaceholderConfigurer类
- 《玩转Spring》第一章 PropertyPlaceholderConfigurer扩展
- PropertyPlaceholderConfigurer扩展
- Spring自定义占位符替换(PropertyPlaceholderConfigurer)
- Spring自定义占位符替换(PropertyPlaceholderConfigurer)
- Spring自定义占位符替换(PropertyPlaceholderConfigurer)
- Spring自定义占位符替换(PropertyPlaceholderConfigurer解析)
- Spring--PropertyPlaceholderConfigurer
- Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
- 自动生成参数表单
- 暴力机构出暴利
- aiml解析之 LINUX C++ RebeccaAIML解析 测试程序 支持中文处理
- Using Screenshot as Texture
- Spring PropertyPlaceholderConfigurer 自定义扩展
- JavaScript String类常用方法比较
- POJ 1125
- 传统的网站css结构面临挑战?
- Squid 基本配置
- 面试关注的点
- vim寄存器与复制粘贴的实现
- IT运维故事:一个IT运维人员的的无奈
- Swing布局管理器介绍