【学习笔记】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&amp;characterEncoding=UTF-8&amp;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应用 - xialiang19851204 - 欢迎来到三件事的空间

0 0
原创粉丝点击