如何在Spring容器中加载自定义的配置文件
来源:互联网 发布:4000三个月淘宝代运营 编辑:程序博客网 时间:2024/05/16 14:05
原文转自:http://www.iitshare.com/spring-container-load-custom-configuration-files.html
写作背景
最近做的项目当中遇到这么一个问题,需要将一些参数配置在一个properties文件中,在项目当中动态获取,频繁使用,由于是频繁使用,为了提高性能,我们就想到在项目初始化的时候将其加载到内存里面,类似加载Spring的配置文件一样,我们最后采用的方式是在Tomcat启动加载数据库配置文件的时候,同时加载我们定义的project.properties配置文件,下面将具体的实现方式写出来,和大家共享下
自定义配置文件
配置文件名为:project.properties,内容如下:
# 是否开启逻辑删除project_del.filter.on=falseproject_domain=http://www.366go.cn/
修改Spring配置文件
之前代码:
<beanid="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <propertyname="locations"> <list> <value>classpath:dbinfo.properties</value> </list> </property></bean>
修改后的配置文件
<beanid="propertyConfigurer" class="com.hisun.core.util.CustomizedPropertyPlaceholderConfigurer"> <propertyname="locations"> <list> <value>classpath:dbinfo.properties</value> <value>classpath:project.properties</value> </list> </property></bean>
加入了classpath:project.properties,其为自定义的配置文件
将PropertyPlaceholderConfigurer类修改为自定义类CustomizedPropertyPlaceholderConfigurer,
PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍
注意下:这个configurer类获取的是所有properties的属性map,如果希望处理某个properties文件,需要在properties中
做一个命名区别,然后在加载的时候,根据key的前缀,进行获取。
定义CustomizedPropertyPlaceholderConfigurer类
类的具体内容为下,
importjava.util.HashMap;importjava.util.Map;importjava.util.Properties; importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer; publicclass CustomizedPropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer { privatestatic Map ctxPropertiesMap; @Override protectedvoid processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap =new HashMap(); for(Object key : props.keySet()) { String keyStr = key.toString();if(keyStr.startsWith("project_")){ String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } } publicstatic Object getContextProperty(String name) { returnctxPropertiesMap.get(name); }}
定义获取配置文件中值的类SpringPropertiesUtil
类的具体内容如下:
importorg.springframework.beans.BeansException;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ApplicationContextAware;importorg.springframework.stereotype.Component; /** * Spring-PropertiesUtil工具类 -获取属性值 * */@Componentpublicclass SpringPropertiesUtil implementsApplicationContextAware { publicstatic final String KEY = "propertyConfigurer"; privatestatic ApplicationContext applicationContext; publicvoid setApplicationContext(ApplicationContext applicationContext) throwsBeansException { SpringPropertiesUtil.applicationContext = applicationContext; } publicstatic ApplicationContext getApplicationContext() { returnapplicationContext; } /** * 获取配置文件中的内容 * * @param keyName * @return */ publicstatic String parseStr(String keyName) { CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext .getBean(KEY); returncp.getContextProperty(keyName).toString(); } /** * 获取配置文件中的内容 * * @param keyName * @return */ publicstatic int parseInt(String keyName) { CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext .getBean(KEY); returnInteger.parseInt(cp.getContextProperty(keyName).toString()); } /** * 获取配置文件中的内容 * * @param keyName * @return */ publicstatic double parseDouble(String keyName) { CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext .getBean(KEY); returnDouble.parseDouble(cp.getContextProperty(keyName).toString()); }}
这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtil.parseStr(“content”)
0 0
- 如何在Spring容器中加载自定义的配置文件
- 如何在Spring容器中加载自定义的配置文件
- spring自定义配置文件的加载
- spring-boot 如何加载rsources下面的自定义配置文件
- 自定义spring容器--spring容器读取bean配置文件的原理
- 监听器如何获取Spring配置文件(加载生成Spring容器)
- 监听器如何获取Spring配置文件(加载生成Spring容器)
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- spring的配置文件在web.xml中加载的方式
- 自定义加载Spring配置文件
- 如何在Listener(监听器)中使用spring容器管理的bean(自定义监听使用spring管理的bean)
- 在自定义Listener中使用Spring容器管理的bean
- Spring的配置文件中加载配置文件
- 如何手动控制spring容器的加载
- 正确地设置安卓通知栏图标尺寸
- Python继承与多继承
- 语音质量评价
- CentOS学习笔记之<虚拟机能ping通同一网关的其他机器,ping不通主机>
- pt-query-digest查询日志分析工具
- 如何在Spring容器中加载自定义的配置文件
- intent启动activity和activity的task启动模式的关系与区别
- 第一次!
- android IPC通信(下)-AIDL
- 《挑战程序设计竞赛》2.2.1 贪心法-区间 POJ2376 POJ1328 POJ3190
- Linux服务器开发初步
- javascript函数执行环境创建过程及解析
- 在IE中使用ActiveX控件,需要使用HTML中的标志是<OBJECT>,该标记几个重要的参数特性有:
- python 自学笔记20151214 定制类