Apache工具类动态加载资源文件

 

    利用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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值