Apache Commons configuration使用入门
2014-02-14 15:48
288 查看
使用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"));
}
}
官网: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使用入门
- Apache Commons configuration使用入门
- Apache Commons configuration使用入门
- Apache Commons Configuration使用入门
- 【转载】使用Apache Commons Configuration读取配置信息
- 使用Apache Commons Configuration读取配置信息
- [j2ee]Apache Commons Configuration 使用
- Apache Commons Configuration 使用
- 使用Apache Commons Configuration读取配置信息
- Apache 项目 之 commons-configuration-1.10.jar 使用 properties配置文件时的编码设置
- Apache Commons DbUtils 使用
- Apache Commons BeanUtils组件介绍使用
- Apache Zeppelin使用入门指南:编程
- apache commons collections CollectionUtils工具类简单使用
- apache commons configuration自动加载修改的配置文件不用重启服务器
- Apache Commons lang组件介绍使用
- apache commons Logging与Log4j结合的使用
- org.apache.commons.io.FileUtils的使用
- 使用org.apache.commons.beanutils个人感受