瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Apache Commons Configuration簡介

          Posted on 2011-06-09 14:56 瘋狂 閱讀(4217) 評論(1)  編輯  收藏 所屬分類: apache項目
          使用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

          評論

          # re: Apache Commons Configuration簡介  回復  更多評論   

          2011-06-15 23:43 by RunCode
          不錯
          主站蜘蛛池模板: 华宁县| 南通市| 花垣县| 湖口县| 武安市| 石林| 乐业县| 岢岚县| 监利县| 临沧市| 岚皋县| 宽甸| 锡林浩特市| 昆明市| 天柱县| 府谷县| 堆龙德庆县| 灵石县| 阳泉市| 海城市| 乌拉特前旗| 宜兴市| 阿尔山市| 本溪市| 自治县| 兰西县| 景宁| 万安县| 阿克| 崇阳县| 清苑县| 高唐县| 山西省| 宝丰县| 武胜县| 安多县| 同心县| 尚义县| 时尚| 平凉市| 满洲里市|