【学习笔记】Spring的PropertyPlaceholderConfigurer应用
来源:互联网 发布:淘宝刷流量会被降权 编辑:程序博客网 时间:2024/06/03 14:04
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
配置如下:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath: conf/sqlmap/jdbc.properties</value>
</list>
</property>
</bean>
譬如,jdbc.properties的内容为:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round;
jdbc.username=root
jdbc.password=123456
那么在spring配置文件中其他配置文件引用变量,我们就可以这样写:
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<property name="driverClassName"value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password"value="${jdbc.password}" />
</bean>
这样,一个简单的数据源就设置完毕了。
查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。
第二部分如果有人想在程序里面获取这个对象怎么办呢。
新增一个类继承他就行,直接在spring配置文件里面替换父亲的配置。
<bean id="propertyConfigurerSon" class="com.jd.printService.service.util.PropertyConfigurerSon">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath: conf/sqlmap/jdbc.properties</value>
</list></property>
</bean>
子类如下
package com.jd.printService.service.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class PropertyConfigurerSon extends PropertyPlaceholderConfigurer {
private final static Logger log = LogManager.getLogger(PropertyConfigurerSon.class);
private static Map<String, Object> ctxPropertiesMap;
protected void processProperties(ConfigurableListableBeanFactory beanFactory,
Properties props) throws BeansException {
super.processProperties(beanFactory, props);
// load properties to ctxPropertiesMap
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
log.error("初始化加载PropertyConfigurerSon文件成功");
}
//static method for accessing context propertiespublic
public static Object getContextProperty(String name){
return ctxPropertiesMap.get(name);
}
}
最后附上PropertyPlaceholderConfigure类继承图
- 【学习笔记】Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- Spring的PropertyPlaceholderConfigurer应用
- JVM java虚拟机内存模型
- 钢条切割问题
- dubbo annocation scan启动过程解析
- socket:10038错误
- java中静态代码块的用法 static用法详解
- 【学习笔记】Spring的PropertyPlaceholderConfigurer应用
- UNIX环境高级编程-第6章- 系统数据文件和信息
- LeetCode:Binary Tree Inorder Traversal
- OC学习笔记(6)内存管理
- cocos2dx使用lua和protobuf
- apt-get安装和 ./configure make make install安装
- CentOS 安装 samba 服务
- Single Number
- PHP 5.3.1 安装包 VC9 VC6 区别是什么