Apache工具类动态加载资源文件
- - 编程语言 - ITeye博客 利用Apache的commons-configuration-1.6.jar 工具类可以实现动态加载XMl,Properties文件,加载原理后续补上. //休息10秒后重新加载配置文件. src/resource/config.xml的路内容是:. 李四 . 已有 0 人发表留言,猛击->> 这里<<-参与讨论.
利用Apache的commons-configuration-1.6.jar 工具类可以实现动态加载XMl,Properties文件,加载原理后续补上。暂时先记录实现方式
package com.test.xml; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; /** * @author Administrator * */ public class TestDybLoadConfig { /** * @param args * @throws ConfigurationException * @throws InterruptedException */ public static void main(String args[]) throws ConfigurationException, InterruptedException{ //动态加载修改过的内容 XMLConfiguration xmlConfig = new XMLConfiguration("./resource/config.xml"); System.out.println( "++read xmlConfig key = " + xmlConfig.getString("Account.name")); xmlConfig.setReloadingStrategy(new FileChangedReloadingStrategy()); //动态加载修改过的内容 PropertiesConfiguration propConfig = new PropertiesConfiguration("./resource/kernel.properties"); propConfig.setReloadingStrategy(new FileChangedReloadingStrategy()); System.out.println( "++read propConfig key = " + propConfig.getString("Password")); //休息10秒后重新加载配置文件 Thread.sleep(10000); System.out.println( "++read xmlConfig key = " + xmlConfig.getString("Account.name")); System.out.println( "++read propConfig key = " + propConfig.getString("Password")); } }
src/resource/config.xml的路内容是:
<?xml version="1.0" encoding="UTF-8"?> <Accounts> <Account type="by0003"> <code>100001</code> <pass>123</pass> <name>李四</name> <money>1000000.00</money> </Account> </Accounts>