zoukankan      html  css  js  c++  java
  • 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"));
        }

    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    测试左移和测试右移
    收藏清单: python测试数据生成及代码扫描最全工具列表
    适合初学者的python实际例子
    Asp.net MVC中提交集合对象,实现Model绑定
    .Net中使用aliases让相同命名空间的dll引用共存
    使用Web.Config Transformation配置灵活的配置文件
    Lazy<T>在Entity Framework中的性能优化实践(附源码)
    菜鸟程序员之Asp.net MVC Session过期异常的处理
    Windows下查看端口占用
    MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息
  • 原文地址:https://www.cnblogs.com/weiguo21/p/4823997.html
Copyright © 2011-2022 走看看