如何在Spring容器中加载自定义的配置文件
1个回答
2016-05-15 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
自定义配置文件
配置文件名为:project.properties,内容如下:
[html] view plain copy
# 是否开启逻辑删除
project_del.filter.on=false
project_domain=
修改Spring配置文件
之前代码:
[html] view plain copy
<beanidbeanid="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertynamepropertyname="locations">
<list>
<value>classpath:dbinfo.properties</value>
</list>
</property>
</bean>
修改后的配置文件
[html] view plain copy
<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 plain copy
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
类的具体内容如下:
[java] view plain copy
importorg.springframework.beans.BeansException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
importorg.springframework.stereotype.Component;
/**
* Spring-PropertiesUtil工具类 -获取属性值
*
*/
@Component
publicclass 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”)
配置文件名为:project.properties,内容如下:
[html] view plain copy
# 是否开启逻辑删除
project_del.filter.on=false
project_domain=
修改Spring配置文件
之前代码:
[html] view plain copy
<beanidbeanid="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertynamepropertyname="locations">
<list>
<value>classpath:dbinfo.properties</value>
</list>
</property>
</bean>
修改后的配置文件
[html] view plain copy
<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 plain copy
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
类的具体内容如下:
[java] view plain copy
importorg.springframework.beans.BeansException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
importorg.springframework.stereotype.Component;
/**
* Spring-PropertiesUtil工具类 -获取属性值
*
*/
@Component
publicclass 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”)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询