如何在Spring容器中加载自定义的配置文件

原文转自:http://www.iitshare.com/spring-container-load-custom-configuration-files.html
写作背景

最近做的项目当中遇到这么一个问题,需要将一些参数配置在一个properties文件中,在项目当中动态获取,频繁使用,由于是频繁使用,为了提高性能,我们就想到在项目初始化的时候将其加载到内存里面,类似加载Spring的配置文件一样,我们最后采用的方式是在Tomcat启动加载数据库配置文件的时候,同时加载我们定义的project.properties配置文件,下面将具体的实现方式写出来,和大家共享下

自定义配置文件

配置文件名为:project.properties,内容如下:

[html]  view plain  copy
  1. # 是否开启逻辑删除  
  2. project_del.filter.on=false  
  3. project_domain=http://www.366go.cn/  


修改Spring配置文件

之前代码:

[html]  view plain  copy
  1. <beanidbeanid="propertyConfigurer"  
  2.     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.     <propertynamepropertyname="locations">  
  4.         <list>  
  5.             <value>classpath:dbinfo.properties</value>  
  6.         </list>  
  7.     </property>  
  8. </bean>  


修改后的配置文件

[html]  view plain  copy
  1. <beanidbeanid="propertyConfigurer"  
  2.     class="com.hisun.core.util.CustomizedPropertyPlaceholderConfigurer">  
  3.     <propertynamepropertyname="locations">  
  4.         <list>  
  5.             <value>classpath:dbinfo.properties</value>  
  6.             <value>classpath:project.properties</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  


加入了classpath:project.properties,其为自定义的配置文件
将PropertyPlaceholderConfigurer类修改为自定义类CustomizedPropertyPlaceholderConfigurer,
PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍

注意下:这个configurer类获取的是所有properties的属性map,如果希望处理某个properties文件,需要在properties中

做一个命名区别,然后在加载的时候,根据key的前缀,进行获取。

定义CustomizedPropertyPlaceholderConfigurer类

类的具体内容为下,

[java]  view plain  copy
  1. importjava.util.HashMap;  
  2. importjava.util.Map;  
  3. importjava.util.Properties;  
  4.    
  5. importorg.springframework.beans.BeansException;  
  6. importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  7. importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
  8.    
  9. publicclass CustomizedPropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer {  
  10.     privatestatic Map ctxPropertiesMap;  
  11.    
  12.     @Override  
  13.     protectedvoid processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,  
  14.                                      Properties props)throws BeansException {  
  15.         super.processProperties(beanFactoryToProcess, props);  
  16.         ctxPropertiesMap =new HashMap();  
  17.         for(Object key : props.keySet()) {  
  18.             String keyStr = key.toString();  
  19.     if(keyStr.startsWith("project_")){  
  20.            String value = props.getProperty(keyStr);  
  21.            ctxPropertiesMap.put(keyStr, value);  
  22.         }  
  23.   
  24.         }  
  25.     }  
  26.     publicstatic Object getContextProperty(String name) {  
  27.         returnctxPropertiesMap.get(name);  
  28.     }  
  29. }  


定义获取配置文件中值的类SpringPropertiesUtil

类的具体内容如下:

[java]  view plain  copy
  1. importorg.springframework.beans.BeansException;  
  2. importorg.springframework.context.ApplicationContext;  
  3. importorg.springframework.context.ApplicationContextAware;  
  4. importorg.springframework.stereotype.Component;  
  5.    
  6. /** 
  7.  * Spring-PropertiesUtil工具类 -获取属性值 
  8.  * 
  9.  */  
  10. @Component  
  11. publicclass SpringPropertiesUtil implementsApplicationContextAware {  
  12.     publicstatic final String KEY = "propertyConfigurer";  
  13.     privatestatic ApplicationContext applicationContext;  
  14.    
  15.     publicvoid setApplicationContext(ApplicationContext applicationContext)  
  16.             throwsBeansException {  
  17.         SpringPropertiesUtil.applicationContext = applicationContext;  
  18.     }  
  19.    
  20.     publicstatic ApplicationContext getApplicationContext() {  
  21.         returnapplicationContext;  
  22.     }  
  23.    
  24.     /** 
  25.      * 获取配置文件中的内容 
  26.      * 
  27.      * @param keyName 
  28.      * @return 
  29.      */  
  30.     publicstatic String parseStr(String keyName) {  
  31.         CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext  
  32.                 .getBean(KEY);  
  33.         returncp.getContextProperty(keyName).toString();  
  34.     }  
  35.    
  36.     /** 
  37.      * 获取配置文件中的内容 
  38.      * 
  39.      * @param keyName 
  40.      * @return 
  41.      */  
  42.     publicstatic int parseInt(String keyName) {  
  43.         CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext  
  44.                 .getBean(KEY);  
  45.         returnInteger.parseInt(cp.getContextProperty(keyName).toString());  
  46.     }  
  47.    
  48.     /** 
  49.      * 获取配置文件中的内容 
  50.      * 
  51.      * @param keyName 
  52.      * @return 
  53.      */  
  54.     publicstatic double parseDouble(String keyName) {  
  55.         CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext  
  56.                 .getBean(KEY);  
  57.         returnDouble.parseDouble(cp.getContextProperty(keyName).toString());  
  58.     }  
  59. }  


这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtil.parseStr(“content”)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值