原文转自:/spring-container-load-custom-configuration-files.html
写作背景
最近做的项目当中遇到这么一个问题,需要将一些参数配置在一个properties文件中,在项目当中动态获取,频繁使用,由于是频繁使用,为了提高性能,我们就想到在项目初始化的时候将其加载到内存里面,类似加载Spring的配置文件一样,我们最后采用的方式是在Tomcat启动加载数据库配置文件的时候,同时加载我们定义的project.properties配置文件,下面将具体的实现方式写出来,和大家共享下
自定义配置文件
配置文件名为:project.properties,内容如下:
[html]view plaincopy#是否开启逻辑删除project_del.filter.on=falseproject_domain=/
修改Spring配置文件
之前代码:
[html]view plaincopy<beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertynamepropertyname="locations"><list><value>classpath:dbinfo.properties</value></list></property></bean>
修改后的配置文件
[html]view plaincopy<beanidbeanid="propertyConfigurer"class="com.hisun.core.util.CustomizedPropertyPlaceholderConfigurer"><propertynamepropertyname="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类
类的具体内容为下,
[java]view plaincopyimportjava.util.HashMap;importjava.util.Map;importjava.util.Properties;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;publicclassCustomizedPropertyPlaceholderConfigurerextendsPropertyPlaceholderConfigurer{privatestaticMapctxPropertiesMap;@OverrideprotectedvoidprocessProperties(ConfigurableListableBeanFactorybeanFactoryToProcess,Propertiesprops)throwsBeansException{super.processProperties(beanFactoryToProcess,props);ctxPropertiesMap=newHashMap();for(Objectkey:props.keySet()){StringkeyStr=key.toString();if(keyStr.startsWith("project_")){Stringvalue=props.getProperty(keyStr);ctxPropertiesMap.put(keyStr,value);}}}publicstaticObjectgetContextProperty(Stringname){returnctxPropertiesMap.get(name);}}
定义获取配置文件中值的类SpringPropertiesUtil
类的具体内容如下:
[java]view plaincopyimportorg.springframework.beans.BeansException;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ApplicationContextAware;importorg.ponent;/***Spring-PropertiesUtil工具类-获取属性值**/@ComponentpublicclassSpringPropertiesUtilimplementsApplicationContextAware{publicstaticfinalStringKEY="propertyConfigurer";privatestaticApplicationContextapplicationContext;publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{SpringPropertiesUtil.applicationContext=applicationContext;}publicstaticApplicationContextgetApplicationContext(){returnapplicationContext;}/***获取配置文件中的内容**@paramkeyName*@return*/publicstaticStringparseStr(StringkeyName){CustomizedPropertyPlaceholderConfigurercp=(CustomizedPropertyPlaceholderConfigurer)applicationContext.getBean(KEY);returncp.getContextProperty(keyName).toString();}/***获取配置文件中的内容**@paramkeyName*@return*/publicstaticintparseInt(StringkeyName){CustomizedPropertyPlaceholderConfigurercp=(CustomizedPropertyPlaceholderConfigurer)applicationContext.getBean(KEY);returnInteger.parseInt(cp.getContextProperty(keyName).toString());}/***获取配置文件中的内容**@paramkeyName*@return*/publicstaticdoubleparseDouble(StringkeyName){CustomizedPropertyPlaceholderConfigurercp=(CustomizedPropertyPlaceholderConfigurer)applicationContext.getBean(KEY);returnDouble.parseDouble(cp.getContextProperty(keyName).toString());}}
这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtil.parseStr(“content”)
如果觉得《如何在Spring容器中加载自定义的配置文件》对你有帮助,请点赞、收藏,并留下你的观点哦!