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 包绝对是 ...
随机推荐
- Swift重写UIButton的图片和标题的位置
import UIKit class ResetBtn: UIButton { let IMAGE_RATIO :CGFloat = 0.7 // 图片占整个按钮高度的比例 let TITLE_FON ...
- CSS样式表的写作规范
推荐大家使用的CSS书写规范.顺序 写了这么久的CSS,但自己都没有按照良好的CSS书写规范来写CSS代码,东写一段西写一段,命名也是想到什么写什么,过一段时间自己都不知道写的是那一块内容, 这样会影 ...
- 求前n项的斐波那契数列、求两个数的最小公倍数、求两个数的最大公约数
class Fib(object): def __call__(self,n): a=[0,1] for i in range(n-2): an ...
- java工程师基础笔试题(一)
一.选择和填空 (不定项哦!) 1,如下是一份文件名为Test2.java的源文件,请问,编译该文件之后会生成几份字节码文件 class Test{ class Inner{} static cla ...
- Ambient Light
[Ambient Light] Ambient light is light that is present all around the scene and doesn’t come from an ...
- ettercap的使用
ettercap -i eth0 -T -M arp:remote -q /<网关地址>// /<目标地址>// arp:remote ,表示双向 使用图形化界面 etterc ...
- shiro 入门
参考文章: https://www.cnblogs.com/maofa/p/6407102.html https://www.cnblogs.com/learnhow/p/9747134.html h ...
- android通过命令行安装sdk
在linux下没有界面化的安装sdk方式,所以需要通过下载zip包或命令行安装 一.通过tools下的android安装 1.进入到android工具 cd $ANDROID_HOME/tools ...
- 【Spider】使用CrawlSpider进行爬虫时,无法爬取数据,运行后很快结束,但没有报错
在学习<python爬虫开发与项目实践>的时候有一个关于CrawlSpider的例子,当我在运行时发现,没有爬取到任何数据,以下是我敲的源代码:import scrapyfrom UseS ...
- Redis备份及回收策略
Redis备份(持久化) Redis备份存在两种方式: 1.一种是"RDB".是快照(snapshotting),它是备份当前瞬间Redis在内存中的数据记录; 2.另一种是&qu ...