commons-configuration读取配置文件


- import org.apache.commons.configuration.CompositeConfiguration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration; //</horsepower>
- </engine-config>
2.读取properties文件
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.PropertiesConfiguration;
- public class PropertiesConfigurationExample {
- public static void main(String[] args) throws Exception {
- Configuration config = new PropertiesConfiguration(
- "com/discursive/jccook/configuration/global.properties" );
- System.out.println( "Speed: " + config.getFloat("speed"));
- System.out.println( "Names: " + config.getString("name"));
- }
- }
- global.properties
- threads.maximum=50
- threads.minimum=20
- timeout=15.52
- interactive=true
- color=red
- speed=50
- name=Default User
- email=default@email.com
- region=Earth
3.当有多个配置文件时,就利用ConfigurationFactory对象来访问多个不同的配置资源
- import java.net.URL;
- import java.util.List;
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationFactory;
- public class PropertiesXmlConfigurationExample {
- public static void main(String[] args) throws Exception {
- PropertiesXmlConfigurationExample example = new PropertiesXmlConfigurationExample();
- ConfigurationFactory factory = new ConfigurationFactory();
- URL configURL = example.getClass().getResource("additional-xml-configuration.xml");
- factory.setConfigurationURL( configURL );
- Configuration config = factory.getConfiguration();
- List startCriteria = config.getList("start-criteria.criteria");
- System.out.println( "Start Criteria: " + startCriteria );
- int horsepower = config.getInt("horsepower");
- System.out.println( "Horsepower: " + horsepower );
- }
- }
- additional-xml-configuration.xml
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <configuration>
- <properties fileName="global.properties"/>
- <xml fileName="global.xml"/>
- </configuration>
4.CompositeConfiguration,手动加上两个配置文件
- import org.apache.commons.configuration.CompositeConfiguration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
- import org.apache.commons.configuration.XMLConfiguration;
- public class Test {
- /**
- * @param args
- * @throws ConfigurationException
- */
- public static void main(String[] args) throws ConfigurationException {
- // TODO Auto-generated method stub
- CompositeConfiguration config = new CompositeConfiguration();
- config.addConfiguration(new PropertiesConfiguration(
- "com/discursive/jccook/configuration/global.properties" ));
- config.addConfiguration( new XMLConfiguration
- ("com/discursive/jccook/configuration/global.xml"));
- List startCriteria = config.getList("start-criteria.criteria");
- int horsepower = config.getInt("horsepower");
- System.out.println( "Start Criteria: " + startCriteria );
- System.out.println(horsepower);
- System.out.println( "Speed: " + config.getFloat("speed"));
- System.out.println( "Names: " + config.getString("name"));
- }
- }
附件列表
commons-configuration读取配置文件的更多相关文章
- 使用Apache Commons Configuration读取配置信息
在项目中使用一些比较新的库总会给你带来很多快乐,在这篇文章中,我将会给你介绍一个在Java中读取配置文件的框架——Apache Commons Configuration framework. 你会了 ...
- Apache Commons Configuration读取xml配置
近期项目自己手写一个字符串连接池.因为环境不同有开发版本.测试版本.上线版本.每一个版本用到的数据库也是不一样的.所以需要能灵活的切换数据库连接.当然这个用maven就解决了.Apache Commo ...
- MVC + EFCore 完整教程19-- 最简方法读取json配置:自定义configuration读取配置文件
问题引出 ASP.NET Core 默认将 Web.config移除了,将配置文件统一放在了 xxx.json 格式的文件中. 有Web.config时,我们需要读到配置文件时,一般是这样的: var ...
- Apache Commons Configuration的应用
Apache Commons Configuration的应用 Commons Configuration是一个java应用程序的配置管理工具.可以从properties或者xml文件中加载软件的配置 ...
- commons configuration管理项目的配置文件
Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...
- ASP.NET Core开发-读取配置文件Configuration
ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...
- NET Core开发-读取配置文件Configuration
ASP.NET Core开发-读取配置文件Configuration ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...
- SpringBoot2 java配置方式 Configuration和PropertySource结合读取配置文件
JdbcConfig.java Configuration是配置文件 PropertySource 引入配置文件 value读取配置文件内容 package cn.itcast.config; imp ...
- ASP.NET Core开发-读取配置文件Configuration appsettings.json
https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...
- 【转】spring boot mybatis 读取配置文件
spring boot mybatis 配置整理 一.加载mybatis的配置 1.手写配置,写死在代码里 import java.io.IOException; import java.util.P ...
随机推荐
- Intel Quick Sync Video Encoder
本篇记录Intel E3 1275处理器集成显卡的硬编码预研过程. 步骤如下: (1)环境搭建 (2)demo编译,测试 (3)研究demo源码,Media SDK API使用 (4)编写so动态库封 ...
- python3保存一个网页
import requests res = requests.get("http://www.baidu.com") savefile = open("baidu.htm ...
- C#实例,熟练使用泛型数组等,课程选择小软件
CourseItem.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- RTMP & HLS
一,直播云架构 2. RTMP 协议 RTMP(Real Time Messaging Protocol)是Adobe Systems公司为Flash播放器和服务器之间音频.视频和数据传输开发的开放协 ...
- c++之helloworld与命名空间
首先在linux中需要安装g++编译器. 在中端输入 uname -a,可以查看版本信息. 输入g++,如果提示错误.则需要使用sudo apt-get install g++. #include&l ...
- Spark Mllib源码分析
1. Param Spark ML使用一个自定义的Map(ParmaMap类型),其实该类内部使用了mutable.Map容器来存储数据. 如下所示其定义: Class ParamMap privat ...
- [算法]Rotate Array
You may have been using Java for a while. Do you think a simple Java array question can be a challen ...
- show processlist,sysbench压力测试工具
processlist.sh 记录数据库的状态 #!/bin/bash while true do mysql -uroot -pwangxiaohu -e 'show processlist\G'| ...
- Spring Cloud之网关搭建
统一由网关进行拦截判断 要不放到每个服务里面就很不合适了 冗余 主要的: <dependency> <groupId>org.springframework.cloud< ...
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...