前面写了一篇关于动态加载属性文件的例子,参考下面的这个帖子:
http://zhangzhenting.iteye.com/blog/600269
针对动态加载属性文件,再次给出一个更简单的例子,去掉了config.xml文件,当然配置也就没有那么灵活了。对于一些简单的配置可以采用这种方式;
在该例子中引入了Spring Quartz的使用,通过quartz来进行测试。
属性动态加载应用:
public class PropertiesHander {
private static PropertiesConfiguration configuration = null;
private static final String CONFIG_FILE = "main.properties";
private PropertiesHander() {
}
public static PropertiesConfiguration getIntance() {
if (configuration == null) {
try {
configuration = new PropertiesConfiguration(CONFIG_FILE);
} catch (ConfigurationException e) {
e.printStackTrace();
}
FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
// 文件自动重加载延时设置为30秒,只有当文件修改之后才会重新加载(根据文件最后修改时间判断)
strategy.setRefreshDelay(30000);
configuration.setReloadingStrategy(strategy);
}
return configuration;
}
public static String getProperty(String propertyName, String defaultValue) {
return getIntance().getString(propertyName, defaultValue);
}
public static String getProperty(String propertyName) {
return getIntance().getString(propertyName);
}
}
Spring Quartz 配置:
<!-- 定时任务 --> <bean id="taskService" class="com.zzt.application.service.TaskService" /> <!--定义目标bean和bean中的方法 --> <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref local="taskService" /> </property> <property name="targetMethod"> <value>execute</value> </property> <!--定义task为单线程--> <property name="concurrent" value="false" /> </bean> <!--定义job触发的时间--> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="jobtask" /> </property> <property name="cronExpression"> <value>5/5 * * * * ?</value> </property> </bean> <!--总管理--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger" /> </list> </property> </bean>
需要引用的包:
commons-collections.jar
commons-configuration-1.6.jar
commons-lang.jar
commons-logging.jar
quartz-1.5.2.jar
spring.jar
附件给出该测试的源码。