Apache Commons Configuration简介

使用Commons  Configuration可以很好的管理我们的配置文件的读写,官网:http://commons.apache.org/configuration/
它支持对配置文件的读取,保存,运行期加载,操作非常方便。下面简单的一下代码示例一下,具体的其他操作文档可以去官方找到:
需要用到commons-lang,commons-collections,commons-logging,log4j jar包

public   class  Test  {
    
    
public   static    void  main(String[] args)  throws  ConfigurationException, InterruptedException  {
        xmlLoadTest();
        fileLoadTest();
        saveTest();
        runtimeReload();
    }

    
// xml文件
     public   static   void  xmlLoadTest()  throws  ConfigurationException {
        String file 
=   " test1.xml " ;
        XMLConfiguration config 
=   new  XMLConfiguration(Test. class .getResource(file));
        System.out.println(config.getString(
" conf.url " ));
        System.out.println(config.getDouble(
" conf.money " ));
    }
  
    
// properties文件
     private   static   void  fileLoadTest()  throws  ConfigurationException  {
        String file 
=   " test2.properties " ;
        PropertiesConfiguration config 
=   new  PropertiesConfiguration(Test. class .getResource(file));
        System.out.println(config.getString(
" url " ));
    }

    
// 保存到文件
     public   static   void  saveTest()  throws  ConfigurationException {
        String file 
=   " test2.properties " ;
        PropertiesConfiguration config 
=   new  PropertiesConfiguration(Test. class .getResource(file));
        
// 设置自动保存 或显示调用 config.save();
        config.setProperty( " colors.background " " #000000 " );
        config.setAutoSave(
true );
    }

    
// 运行期参数修改加载
     public   static   void  runtimeReload()  throws  ConfigurationException, InterruptedException {
        String file 
=   " test2.properties " ;
        PropertiesConfiguration config 
=   new  PropertiesConfiguration(Test. class .getResource(file));
        config.setReloadingStrategy(
new  FileChangedReloadingStrategy());
        System.out.println(config.getString(
" url " ));
        Thread.sleep(
10000 ); // 在休眠期间,手动修改文件里面的url值后观察日志情况
        System.out.println(config.getString( " url " ));
    }

}


Configuration 的参数可能来自下面的资源: 
     Properties files XML documents,Property list files (.plist),JNDI,JDBC Datasource,System properties, Applet parameters,Servlet parameters

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值