使用Apache Commons Configuration读取配置信息
来源:互联网 发布:可以看亚丝娜本子软件 编辑:程序博客网 时间:2024/04/20 15:15
你会了解到
·在程序改变后自动重新加载配置。
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.1config.getString("database.url");// 1521config.getString("database.port");
1
getBoolean
2
getByte
3
getDouble
4
getFloat
5
getInt
6
getInteger
7
getList
8
getLong
9
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.1config.getString("databases.database(0).url"); // 192.23.44.100config.getString("databases.database(1).url");
XPath表达式
XMLConfiguration config =new XMLConfiguration("const.xml");config.setExpressionEngine(new XPathExpressionEngine()); // 127.0.0.1config.getString("databases/database[name = 'dev']/url"); // 192.23.44.100config.getString("databases/database[name = 'production']/url");
这里是上面两个XPath表达式查询的一个解释:
访问环境变量
1
EnvironmentConfiguration config =
new
EnvironmentConfiguration();
2
config.getString(
"ENV_TYPE"
);
1
echo
%ENV_TYPE%
# for Windows
2
# or...
3
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中。)
- 使用Apache Commons Configuration读取配置信息
- Apache Commons Configuration 读取配置信息
- Apache Commons Configuration读取xml配置
- 使用Apache commons-configuration读取配置文件
- Apache Commons Configuration 使用
- [j2ee]Apache Commons Configuration 使用
- Apache Commons configuration使用入门
- Apache Commons Configuration使用入门
- Apache Commons Configuration使用入门
- Apache Commons Configuration使用入门
- 使用commons的Configuration读取XML
- 配置文件读取类:commons-configuration使用
- 使用commons的Configuration读取XML
- apache commons 之 commons-configuration
- Apache Commons-configuration 范例
- Apache Commons Configuration
- commons.configuration 读取示例
- 使用apache commons包读取配置文件【转】
- 天分决定速度,勤奋决定高度
- sublime text3配置方法、插件推荐及使用技巧
- Stanford CoreNlp中英文Java API使用方法
- C#调用支付宝接口案例
- MD5算法原理
- 使用Apache Commons Configuration读取配置信息
- 欢迎使用CSDN-markdown编辑器
- Java 枚举
- 在Docker容器中运行Spring Boot应用
- 关系表达式
- mysql数据库基础知识
- 建立一个带有头结点的单向链表,并将存储在数组中的字符依次转储到链表的各个结点中
- R-CNN 简单梳理
- SpringMVC 之 后台接收参数 与前台传递数据