一、应用场景:项目中需要频繁变更配置参数时,可以设置定时读取配置参数完成配置参数的及时加载
二、代码实现:
1.配置类
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Component;
@Component("gameConfig")
public class GameConfig {
private static Map<String, String> gameConfigs = new ConcurrentHashMap<String, String>();
public static Map<String, String> getGameConfigs() {
return gameConfigs;
}
public static void setGameConfigs(String key, String value) {
GameConfig.gameConfigs.put(key, value);
}
@Override
public String toString() {
//方便看结果,重写个toString方法
return "GameConfig [gameConfigs=" + gameConfigs.get("rollspeed")+ "]";
}
}
2.文件读取即定时刷新配置
/**
* @author Jupiter
* @description 读取配置文件的类
* @date 2018年12月10日
*/
public class LoadPropertiesConfig extends PropertyPlaceholderConfigurer{
/**
* PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。
* PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。
* XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
*
* processProperties方法的重写,可以
*/
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
//createReFreshTask(props);
createReFreshTask();
}
/**
* @description 刷新配置文件的任务
* @date 2018年12月10日
* @param props
*/
private void createReFreshTask() {
//获取要刷新的配置文件路径
String path = LoadPropertiesConfig.class.getResource("/").getPath()+"/conf/system.properties";
//System.out.println(path);
try {
//获取路径下的配置文件
PropertiesConfiguration config = new PropertiesConfiguration(path);
String readPeriod = config.getString("readPeriod","2");//读取时间间隔,单位秒
//创建定时读取这个配置文件的任务
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//获取要刷新的配置文件路径
String path = LoadPropertiesConfig.class.getResource("/").getPath()+"/conf/system.properties";
//System.out.println(path);//获取路径下的配置文件
PropertiesConfiguration config;
try {
config = new PropertiesConfiguration(path);
//读取配置文件中会随需求改变的值,假设名字叫转动速度
String rollspeed = config.getString("rollspeed","120");
System.out.println("定时读取配置文件中.....");
//给全局变量的设值
GameConfig.setGameConfigs("rollspeed",rollspeed);
System.out.println("定时任务读到的rollspeed:"+rollspeed);
} catch (ConfigurationException e) {
System.out.println("加载文件异常");
}
}
}, 0, Long.parseLong(readPeriod)*1000);
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
+.坑:①注意路径;②在run方法中需要重新获取路径!!!
3.定时刷新测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TimerReadTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"classpath:/conf/*.xml"});
while(true) {
System.out.println(applicationContext.getBean("gameConfig").toString());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}
}
}
}
4.配置文件
放在src/main/resources/conf下,applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean class="com.juwenzhe.wx.test.LoadPropertiesConfig">
<property name="locations">
<array>
<value>classpath:/conf/system.properties</value>
</array>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
<context:component-scan base-package="com.juwenzhe.wx.test"/>
<task:scheduler id="timerScheduler" pool-size="10"/>
<task:annotation-driven scheduler="timerScheduler"/>
</beans>
在src/main/resources/conf下,system.properties
readPeriod=2
rollspeed=666
5.测试结果
参考文献