Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 11813

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

$
0
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>

 

 



已有 0人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐




Viewing all articles
Browse latest Browse all 11813

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>