SpringMVC用PropertyPlaceholderConfigurer读取配置文件
来源:互联网 发布:手机虚拟试衣软件 编辑:程序博客网 时间:2024/05/29 04:57
在使用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;@Componentpublic class PropertyReloader {@Resourceprivate LoadPropertyPlaceholderConfigurer loadPropertyPlaceholderConfigurer;@Scheduled(initialDelay = 5 * 60 * 1000,fixedDelay = 5 * 60 * 1000) public void reload(){loadPropertyPlaceholderConfigurer.reload();}}
阅读全文
0 0
- SpringMVC用PropertyPlaceholderConfigurer读取配置文件
- springMVC框架中多个配置文件使用PropertyPlaceholderConfigurer读取配置properties文件
- 使用PropertyPlaceholderConfigurer读取配置文件
- PropertyPlaceholderConfigurer读取配置文件
- SpringMVC 读取配置文件
- <value>${jdbc.driverClassName}</value>解读——PropertyPlaceholderConfigurer读取配置文件
- springmvc 通过注解读取配置文件
- SpringMVC中如何读取配置文件中的值
- springmvc如何通过properties读取配置文件
- springMVC中读取配置文件连接数据库
- Spring PropertyPlaceholderConfigurer初始化properties配置文件
- spring管理配置文件之PropertyPlaceholderConfigurer
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
- 用Python读取配置文件
- 用ResourceBundle 读取配置文件
- PropertyPlaceholderConfigurer属性配置文件 资源文件绑定
- 继承PropertyPlaceholderConfigurer 实现动态加载配置文件
- SpringMVC配置文件读取其他配置文件中属性VS类中注入配置文件属性
- bzoj4726 [ POI2017 ] -- 树形DP
- 线程同步与互斥
- bzoj2120&&2453 -- 带修改莫队
- bzoj4546 -- 可持久化字典树
- bzoj4712 -- 树链剖分
- SpringMVC用PropertyPlaceholderConfigurer读取配置文件
- Oracle数字函数
- bzoj2588 -- 树链剖分+主席树
- bzoj4881 [ Lydsy2017年5月月赛 ] -- 二分图染色+线段树
- bzoj2165 -- 倍增floyd
- bzoj3626 [ LNOI2014 ] -- 树链剖分
- POJ2104_K-th Number_归并树|二分 ||平方分割的分桶法|二分
- SVN导下来Maven工程就报错 org.apache.ibatis.binding.BindingException
- bzoj1857 [ SCOI2010 ] -- 三分套三分