浅谈Spring的PropertyPlaceholderConfigurer

1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。

2.Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:

 <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.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 对于读取一个配置文件采取的方案 -->
        <property name="location" value="classpath:cfg.properties"></property>
        <!-- 对于读取两个以上配置文件采取的处理方案 -->
        <!--
        <property name="locations"> 
            <list>
                <value>classpath:cfg.properties</value>
                <value>classpath:cfg2.properties</value>
            </list>
        </property>
        -->
    </bean>
    <!--采用这种方式简化配置文件-->
    <!--  <context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/> -->
    
    <!--同时,我们也可以使用下面这种配置方式进行配置,这里我配NEVER的意思是不读取系统配置信息。
    <context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties" system-properties-mode="NEVER"/>
     -->
</beans>

3.PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。

4.查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。

PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在JavaSystem类属性中查找。

我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

 实例:

<!-- 引入配置文件 -->
	<bean id="PropertyPlaceholderConfigurer" class="com.common.services.PropertiesHelper">
		<property name="ignoreResourceNotFound" value="false" /> 
		<property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:config.properties</value>
            </list>
        </property>
    </bean>	
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.ObjectUtils;

/**
 * 获取properties配置文件文件的参数
 * @author uncle bad
 *
 */
public class PropertiesHelper extends PropertyPlaceholderConfigurer {
	private static Map<String,String> ctxPropertiesMap;  
	
	@Override  
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		ctxPropertiesMap = new HashMap<String,String>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			ctxPropertiesMap.put(keyStr, value);
		}
	}
	
	/**
	 * 获取上下文中的properties属性参数值
	 * 如果没有包含该keyName值返回null
	 * @param name
	 * @return
	 */
	public static String getContextProperty(String keyName) {
		if(ObjectUtils.isEmpty(ctxPropertiesMap)){
			return null;
		}
		if(ctxPropertiesMap.containsKey(keyName))
			return ctxPropertiesMap.get(keyName);
		else
			return null;
	}
	
	/**
	 * 获取所有properties配置文件文件的name
	 * @return
	 */
	public static String[] getPropertyNames(){
		if(ObjectUtils.isEmpty(ctxPropertiesMap)){
			return new String[0];
		}
		Set<String> keys = ctxPropertiesMap.keySet();
		String names [] = new String[keys.size()];
		Iterator<String> iterator = keys.iterator();
		int i = 0;
		while(iterator.hasNext()){
			names[i] = iterator.next().toString();
			i++;
		}
		return names;
	}
	/**
	 * 获取所有properties配置文件的参数值
	 * @return
	 */
	public static Map<String,String> getCTXPropertiesMap(){
		if(ObjectUtils.isEmpty(ctxPropertiesMap)){
			return new HashMap<String,String>();
		}
		return ctxPropertiesMap;
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值