使用Apache Commons Configuration读取配置信息
2019独角兽企业重金招聘Python工程师标准>>>
在项目中使用一些比较新的库总会给你带来很多快乐,在这篇文章中,我将会给你介绍一个在Java中读取配置文件的框架——Apache Commons Configuration framework.
你会了解到
·在程序改变后自动重新加载配置。
Maven设置
<dependencies><dependency><groupId>commons-configuration</groupId><artifactId>commons-configuration</artifactId><version>1.8</version></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.8.0</version></dependency><dependency><groupId>commons-jxpath</groupId><artifactId>commons-jxpath</artifactId><version>1.3</version></dependency>
</dependencies>
简单的数据库配置
<?xmlversion="1.0"encoding="UTF-8"?>
<!-- const.xml -->
<config><database><url>127.0.0.1</url><port>1521</port><login>admin</login><password>pass</password></database>
</config>
XMLConfiguration config =new XMLConfiguration("const.xml");
// 127.0.0.1
config.getString("database.url");
// 1521
config.getString("database.port");
getBoolean
getByte
getDouble
getFloat
getInt
getInteger
getList
getLong
getStringArray
将如上配置扩展一步
<?xmlversion="1.0"encoding="UTF-8"?>
<!-- const.xml -->
<config><databases><database><name>dev</name><url>127.0.0.1</url><port>1521</port><login>admin</login><password>pass</password></database><database><name>production</name><url>192.23.44.100</url><port>1521</port><login>admin</login><password>not-so-easy-pass</password></database></databases>
</config>
XMLConfiguration config =new XMLConfiguration("const.xml");// 127.0.0.1
config.getString("databases.database(0).url");// 192.23.44.100
config.getString("databases.database(1).url");
XPath表达式
XMLConfiguration config =new XMLConfiguration("const.xml");
config.setExpressionEngine(new XPathExpressionEngine());// 127.0.0.1
config.getString("databases/database[name = 'dev']/url"); // 192.23.44.100
config.getString("databases/database[name = 'production']/url");
访问环境变量
EnvironmentConfiguration config =new EnvironmentConfiguration();
config.getString("ENV_TYPE");
echo %ENV_TYPE% # for Windows
# or...
echo $ENV_TYPE # for Linux/Mac OS
联合配置
public String getDbUrl() throws ConfigurationException {EnvironmentConfiguration envConfig =new EnvironmentConfiguration();String env = envConfig.getString("ENV_TYPE");if("dev".equals(env) ||"production".equals(env)) {XMLConfiguration xmlConfig =new XMLConfiguration("const.xml");xmlConfig.setExpressionEngine(new XPathExpressionEngine());String xpath ="databases/database[name = '"+ env +"']/url";return xmlConfig.getString(xpath);}else{String msg ="ENV_TYPE environment variable is "+"not properly set";throw new IllegalStateException(msg);}
}
集中你的配置
<?xmlversion="1.0"encoding="UTF-8"?>
<!-- config.xml -->
<configuration><env/><xmlfileName="const.xml"/>
</configuration>
public String getDbUrl()throws ConfigurationException {DefaultConfigurationBuilder builder =new DefaultConfigurationBuilder("config.xml");boolean load =true;CombinedConfiguration config = builder.getConfiguration(load);config.setExpressionEngine(new XPathExpressionEngine());String env = config.getString("ENV_TYPE");if("dev".equals(env) ||"production".equals(env)) {String xpath ="databases/database[name = '"+ env +"']/url";return config.getString(xpath);}else{String msg ="ENV_TYPE environment variable is "+"not properly set";throw new IllegalStateException(msg);}
}
自动重新加载
XMLConfiguration config =new XMLConfiguration("const.xml");
ReloadingStrategy strategy =new FileChangedReloadingStrategy();
strategy.setRefreshDelay(5000);
config.setReloadingStrategy(strategy);
<?xmlversion="1.0"encoding="UTF-8"?>
<!-- config.xml -->
<configuration><env/><xmlfileName="const.xml"><reloadingStrategyrefreshDelay="5000"config-class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"/></xml>
</configuration>
最后
·自动解析配置文件的实际路径(不管你是把它放在程序文件夹下还是一个jar中。)
OSchina.NET原创翻译/原文链接
转载于:https://my.oschina.net/jack230230/blog/57171
使用Apache Commons Configuration读取配置信息相关推荐
- JavaWEB中读取配置信息
第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ...
- Apache Commons Configuration使用入门
项目开发过程中不免有许多在运行过程中需要经常改动的参数,比如说数据库的连接参数等.把这些需要经常改动的参数保存在配置文件中,让用户可以修改,这样既增强了项目的适应能力,也减少了我们的维护成本.这种做法 ...
- 『Java CVE』CVE-2022-33980: Apache Commons Configuration 读文件RCE
文章目录 影响版本 漏洞原理(机翻自CVE页面) 漏洞复现 环境配置 jdk版本 pom.xml 基本使用demo PoC 代码审计 对生产环境的影响 漏洞修复 参考 完 影响版本 Apache Co ...
- ASP.NET Core的配置(1):读取配置信息
提到"配置"二字,我想绝大部分.NET开发人员脑海中会立马浮现出两个特殊文件的身影,那就是我们再熟悉不过的app.config和web.config,多年以来我们已经习惯了将结构化 ...
- intellij中出現java.lang.NoClassDefFoundError: org/apache/commons/configuration/Configuration解決方案
完整报错如下: Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties 19/03/06 19: ...
- 在.net中使用强类型来读取配置信息
在.net应用中,配置信息以XML文档的形式来存储.依.net的惯例,web.config为Asp.net应用(包括Web Service应用)提供配置信息:而*.exe.config为可执行的应用( ...
- php+读取配置里的值,ThinkPHP 读取配置信息与动态配置(C方法)
ThinkPHP C 方法 C 方法是 ThinkPHP 内置的特殊方法,用于在操作和模板里读取配置文件里的配置信息或者动态配置参数.语法如下: C( para_name, value ) 参数说明 ...
- 深入探究.Net Core Configuration读取配置的优先级
前言 在之前的文章.Net Core Configuration源码探究一文中我们曾解读过Configuration的工作原理,也.Net Core Configuration Etcd数据源一文中探 ...
- java getenv_Java利用System.getenv()和System.getProperty()来读取配置信息
System.getenv()和System.getProperty()是Java种常用的两种读取环境配置信息的方法,它们都是java.lang.System类的方法,用途有相似之处,但我们在实际使用 ...
最新文章
- 前端:JS中JSON对象和String转换
- NHibenate 一些自带增删改查
- Foundation框架: 11.NSDate的基本认识和使用
- 如何理解Java中参数传递只能传值?
- Java入门算法(动态规划篇2:01背包精讲)
- Lighting System Design UVA 11400 (dp+思维)
- 在 Dubbo3.0 上服务治理的实践
- win7系统去除图标箭头的方法
- 浅谈C++的智能指针
- webpack教程(二)——webpack.config.js文件
- http://bassistance.de/jquery-plugins/
- android对象引用释放,Android程序的内存泄漏与规避方法
- JDBC的下载和安装教程
- 小新黑苹果声卡ID注入
- ubuntu下使用pip卸载包时出现Cannot uninstall scipy
- 后门防御阅读笔记,Black-box Detection of Backdoor Attacks with Limited Information and Data
- 简单实现点击图片放大的功能
- 科技型中小企业的申报流程及材料?
- SecureFX 查看隐藏文件夹
- A Survey of Two Signature Aggregation Techniques学习笔记