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"));
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Apache Commons configuration使用入门的更多相关文章
- 使用Apache Commons Configuration读取配置信息
在项目中使用一些比较新的库总会给你带来很多快乐,在这篇文章中,我将会给你介绍一个在Java中读取配置文件的框架——Apache Commons Configuration framework. 你会了 ...
- Apache Commons Configuration读取xml配置
近期项目自己手写一个字符串连接池.因为环境不同有开发版本.测试版本.上线版本.每一个版本用到的数据库也是不一样的.所以需要能灵活的切换数据库连接.当然这个用maven就解决了.Apache Commo ...
- Apache Commons Configuration的应用
Apache Commons Configuration的应用 Commons Configuration是一个java应用程序的配置管理工具.可以从properties或者xml文件中加载软件的配置 ...
- Apache Commons IO入门教程(转)
Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...
- [转]Apache Commons IO入门教程
Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...
- commons configuration管理项目的配置文件
Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...
- Apache Commons 常用工具类整理
其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...
- Commons Configuration之二基本特性和AbstractConfiguration
Configuration接口定义一大堆方法.一切从头开始实现这些方法非常困难.因为AbstractConfiguration类存在.该类在Commons Configuration中充当大多数Con ...
- apache commons io入门
原文参考 http://www.javacodegeeks.com/2014/10/apache-commons-io-tutorial.html Apache Commons IO 包绝对是 ...
随机推荐
- CentOS 7下源码安装zabbix服务
安装环境需要LAMP或者LNMP先搭建好 在此我使用上一篇搭建好的LNMP环境来安装zabbix 1.下载zabbix http://www.zabbix.com/download.php 2.安装及 ...
- Apache tica详述
Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...
- Oracle 连接数据库
使用的DLL:Oracle.ManagedDataAccess Bug:OracleInternal.Common.ProviderConfig的类型初始值设定项引发异常 App.config的更改才 ...
- Redis安装完后redis-cli无法使用(redis-cli: command not found)已使用
wget http://download.redis.io/redis-stable.tar.gz(下载redis-cli的压缩包) tar xvzf redis-stable.tar.gz(解压) ...
- MySQL 5.7 使用原生JSON类型
首先回顾一下JSON的语法规则: 数据在键值对中, 数据由逗号分隔, 花括号保存对象, 方括号保存数组. 按照最简单的形式,可以用下面的JSON表示: {"NAME": " ...
- javascript学习笔记(九):DOM操作HTML的各种方法使用
<!DOCTYPE html> <html> <head lang="en"> <meta chaset="UTF-8" ...
- db2 性能查看top sql
DB2 V10.3 查看top sql ,类似oracle 这篇文章是对之前有篇db2 v9的简化更新,总体还是觉得DB2TOP比较好用直观,不过需要导出SQL时,或自动化脚本时,还是建议执行S ...
- 【MongoDB】关于无法连接mongo的问题
今天使用MongoDB的时候发现直接输入mongo提示连接失败 首先想到的可能是服务还没启动 当我随便打开一个cmd输入net start MongoDB 提示:net start mongodb 发 ...
- IronPython 的几个问题
1.在脚本中使用datagridview.Rows[i].Cells[1].Value并将其转换为string时,遇到int类型 有时可是直接使用.toString()转换为字符 有时必须采用str( ...
- openstack(Pike 版)集群部署(五)--- Neutron 部署
一.介绍 参照官网部署:https://docs.openstack.org/neutron/pike/install/install-rdo.html 继续上一博客进行部署:http://ww ...