Spring如何加载变化的配置文件
Spring在启动时将所有的配置文件加载进去。如果Spring加载的配置文件*.properties修改了,那我如何在不重启服务的情况下能让spring读到最新的*.pr...
Spring在启动时将所有的配置文件加载进去。如果Spring加载的配置文件*.properties修改了,那我如何在不重启服务的情况下能让spring读到最新的*.properties呢?
(因为服务在线上,所以我不能用重启服务来解决) 展开
(因为服务在线上,所以我不能用重启服务来解决) 展开
1个回答
展开全部
配置文件名为:project.properties,内容如下:
# 是否开启逻辑删除
del.filter.on=false
domain=http://www.366go.cn/
修改Spring配置文件
之前代码:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbinfo.properties</value>
</list>
</property>
</bean>
修改后的配置文件
<bean id="propertyConfigurer"
class="com.hisun.core.util.CustomizedPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbinfo.properties</value>
<value>classpath:project.properties</value>
</list>
</property>
</bean>
加入了classpath:project.properties,其为自定义的配置文件
将PropertyPlaceholderConfigurer类修改为自定义类CustomizedPropertyPlaceholderConfigurer,
PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍
定义CustomizedPropertyPlaceholderConfigurer类
类的具体内容为下,
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class CustomizedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private static Map ctxPropertiesMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}
定义获取配置文件中值的类SpringPropertiesUtil
类的具体内容如下:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring-PropertiesUtil工具类 -获取属性值
*
*/
@Component
public class SpringPropertiesUtil implements ApplicationContextAware {
public static final String KEY = "propertyConfigurer";
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringPropertiesUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public static String parseStr(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return cp.getContextProperty(keyName).toString();
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public static int parseInt(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return Integer.parseInt(cp.getContextProperty(keyName).toString());
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public static double parseDouble(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return Double.parseDouble(cp.getContextProperty(keyName).toString());
}
}
这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtil.parseStr(“content”)
# 是否开启逻辑删除
del.filter.on=false
domain=http://www.366go.cn/
修改Spring配置文件
之前代码:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbinfo.properties</value>
</list>
</property>
</bean>
修改后的配置文件
<bean id="propertyConfigurer"
class="com.hisun.core.util.CustomizedPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbinfo.properties</value>
<value>classpath:project.properties</value>
</list>
</property>
</bean>
加入了classpath:project.properties,其为自定义的配置文件
将PropertyPlaceholderConfigurer类修改为自定义类CustomizedPropertyPlaceholderConfigurer,
PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍
定义CustomizedPropertyPlaceholderConfigurer类
类的具体内容为下,
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class CustomizedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private static Map ctxPropertiesMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}
定义获取配置文件中值的类SpringPropertiesUtil
类的具体内容如下:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring-PropertiesUtil工具类 -获取属性值
*
*/
@Component
public class SpringPropertiesUtil implements ApplicationContextAware {
public static final String KEY = "propertyConfigurer";
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringPropertiesUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public static String parseStr(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return cp.getContextProperty(keyName).toString();
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public static int parseInt(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return Integer.parseInt(cp.getContextProperty(keyName).toString());
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public static double parseDouble(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return Double.parseDouble(cp.getContextProperty(keyName).toString());
}
}
这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtil.parseStr(“content”)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询