Apache Commons Configuration的应用
Commons Configuration是一个java应用程序的配置管理工具。可以从properties或者xml文件中加载软件的配置信息,用来构建支撑软件运行的基础环境。在一些配置文件较多较的复杂的情况下,使用该配置工具比较可以简化配置文件的解析和管理。也提高了开发效率和软件的可维护性。
一、介绍
官方列举Commons Configuration的主要功能:
Configuration parameters may be loaded from the following sources:
Properties files 
XML documents 
Windows INI files 
Property list files (plist) 
JNDI 
JDBC Datasource 
System properties 
Applet parameters 
Servlet parameters
Commons Configuration所依赖的包
Component Dependencies
Core Java 1.3
commons-collections
commons-lang
commons-logging
ConfigurationFactory commons-digester
commons-beanutils
Java 1.4 or xml-apis
DefaultConfigurationBuilder commons-beanutils
Java 1.4 or (xml-apis + xerces + xalan)
DatabaseConfiguration JDBC 3.0 (Java 1.4 or jdbc2_0-stdext.jar)
XMLConfiguration Java 1.4 or (xml-apis + xerces + xalan)
XMLPropertiesConfiguration Java 1.4 or (xml-apis + xerces)
PropertyListConfiguration commons-codec
XMLPropertyListConfiguration commons-codec
Java 1.4 or xml-apis
ConfigurationDynaBean commons-beanutils
XPathExpressionEngine commons-jxpath
EnvironmentConfiguration Java 1.5 or ant 1.6.5
 
二、给出一个简单的例子
 
package cfgtest;

import org.apache.commons.configuration.*;

/** 
* Commons Configuration读取属性文件的例子 

* @author leizhimin 2008-9-23 9:40:17 
*/ 
public class Test1 { 
        public static void main(String[] args) throws ConfigurationException { 
                test1(); 
        }

public static void test1() throws ConfigurationException { 
                 
                CompositeConfiguration config = new CompositeConfiguration(); 
                //config.addConfiguration(new SystemConfiguration()); 
                config.addConfiguration(new PropertiesConfiguration("cfgtest/test1.properties")); 
                 
                String usernaem = config.getString("username"); 
                String password = config.getString("password"); 
                 
                System.out.println(usernaem + " " + password);


}

 
cfgtest/test1.properties
username = lavasoft 
password = leizhimin 
 
运行结果:
lavasoft leizhimin

Process finished with exit code 0

 
从上面看出,使用Apache Commons Configuration来读取配置确实很简单,还可以省很多事情。它不光可以读取properties文件,还可以读取xml,还可以读取xml和properties混合文件等等。

Apache Commons Configuration的应用的更多相关文章

  1. 使用Apache Commons Configuration读取配置信息

    在项目中使用一些比较新的库总会给你带来很多快乐,在这篇文章中,我将会给你介绍一个在Java中读取配置文件的框架——Apache Commons Configuration framework. 你会了 ...

  2. Apache Commons Configuration读取xml配置

    近期项目自己手写一个字符串连接池.因为环境不同有开发版本.测试版本.上线版本.每一个版本用到的数据库也是不一样的.所以需要能灵活的切换数据库连接.当然这个用maven就解决了.Apache Commo ...

  3. Apache Commons configuration使用入门

    使用Commons  Configuration可以很好的管理我们的配置文件的读写, 官网:http://commons.apache.org/configuration 需要用到commons-la ...

  4. commons configuration管理项目的配置文件

    Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...

  5. Apache Commons 常用工具类整理

    其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...

  6. Commons Configuration之二基本特性和AbstractConfiguration

    Configuration接口定义一大堆方法.一切从头开始实现这些方法非常困难.因为AbstractConfiguration类存在.该类在Commons Configuration中充当大多数Con ...

  7. Apache Commons CLI命令行启动

    今天又看了下Hangout的源码,一般来说一个开源项目有好几种启动方式--比如可以从命令行启动,也可以从web端启动.今天就看看如何设计命令行启动... Apache Commons CLI Apac ...

  8. 编写更少量的代码:使用apache commons工具类库

    Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commo ...

  9. Apache Commons 工具集

    一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是 ...

随机推荐

  1. Lightoj1014【基础题】

    题意: 有C个人,安排了P个吃的,每个人会吃Q个吃的,最后留下L个吃的:求所有可能的Q,从小到大输出,要保证Q>L; 思路: 其实就是求出P-L的所有数的约数,然后这个约数>L的话就满足: ...

  2. 使用dynamic关键词 CS1969错误

    添加 Microsoft.CSharp.dll 引用即可 不需要添加using Microsoft.CSharp 这类namespace

  3. 51nod1625(枚举&贪心)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...

  4. 构建使用SQL服务器的ASP.net Core2.0 API

    web api的教程非常少,使用 core2.0的更少,微软提供了一个aspnet core2的教程,也提供了EF core中访问SQL服务器的教程,参考这些教程可以做出使用sql server的as ...

  5. 基于.net core微服务(Consul、Ocelot、Docker、App.Metrics+InfluxDB+Grafana、Exceptionless、数据一致性、Jenkins)

    1.微服务简介 一种架构模式,提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相沟通(RESTfu ...

  6. corn表达式 经典

    https://www.cnblogs.com/GarfieldTom/p/3746290.html

  7. 再看SpringMVC通过一个DispatcherServlet处理Servlet

    初始入口: org.springframework.web.context.ContextLoaderListener org.springframework.web.context.ContextL ...

  8. React项目搭建(脚手架)

    首先我们需要安装node环境:download nodejs:https://i.cnblogs.com/EditPosts.aspx?opt=1 找到你需要的版本和系统安装包下载并安装. 这时候你可 ...

  9. 自定义Mega菜单的巧妙实现

    查看了<云制造>官网源码,为其mega菜单的巧妙实现打call. 其另辟蹊径,采取父级主控分支的方法,仅对父级“增加/删除”控制标识,从而控制子层显示. <!DOCTYPE html ...

  10. 安装AAA服务器遇到的问题

    安装升级AAA服务器的操作思路: 第一种:在原始服务器下更新升级安装包,实现升级: 第二种:重新安装最新版本的IOS,实现版本更新: 第一种方案更新AAS服务器ios 步骤1:copy软件更新包到li ...