SpringMVC用PropertyPlaceholderConfigurer读取配置文件

在使用SpringMVC做Web开发的时候,为了便于统一管理配置项,常常会看到用占位符的配置方式。这样,可以将分散在spring配置文件中的配置项的值集中到一个(多个)属性文件中,方便管理。

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

<bean id="loadPropertyPlaceholderConfigurer"
		class="com.gx.cms.api.filter.LoadPropertyPlaceholderConfigurer"
		lazy-init="false">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
<!-- 				<value>file:config/url.properties</value> -->
                   <value>classpath:/config/url.properties</value>
			</list>
		</property>
	</bean>

重写PropertyPlaceholderConfigurer 

package com.gx.cms.api.filter;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class LoadPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	
	private static Map<String, Object> ctxPropertiesMap; 
	 
    @Override 
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException { 
        super.processProperties(beanFactoryToProcess, props); 
        ctxPropertiesMap = new HashMap<String, Object>(); 
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr); 
            ctxPropertiesMap.put(keyStr, value); 
        }
//        try {
//			System.out.println("1111-"+new ObjectMapper().writeValueAsString(props));
//			System.out.println("1111-"+new ObjectMapper().writeValueAsString(ctxPropertiesMap));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
    } 
 
    public static <T> T getContextProperty(String name) {
        return (T)ctxPropertiesMap.get(name); 
    } 
    
    @SuppressWarnings("all")
	public void reload(){
    	try{
    		Properties result = mergeProperties();
//    		logger.error("reload properties!"+result);
    		HashMap nc =new HashMap<String, Object>();
    		nc.putAll(result);
    		ctxPropertiesMap=nc;
//    		try {
//    			System.out.println("22-"+new ObjectMapper().writeValueAsString(nc));
//    			System.out.println("222-"+new ObjectMapper().writeValueAsString(ctxPropertiesMap));
//    		} catch (Exception e) {
//    			e.printStackTrace();
//    		}
    	}catch(Exception ex){
    		logger.error("reload properties error!",ex);
    	}
    }
    
}
定时刷新配置文件

package com.gx.cms.api.filter;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class PropertyReloader {

	@Resource
	private LoadPropertyPlaceholderConfigurer loadPropertyPlaceholderConfigurer;
	
	@Scheduled(initialDelay = 5 * 60 * 1000,fixedDelay = 5 * 60 * 1000)
    public void reload(){
		loadPropertyPlaceholderConfigurer.reload();
	}

	
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值