第六十四篇:commons-configuration使用介绍

commons-configuration是Apache开源组织提供的用于操作配置文件的工具包。配置参数的来源可以是:Properties files、XML documents、Windows INI files、Property list files (plist)、JNDI、JDBC Datasource、System properties、Applet parameters和Servlet parameters。 
下面为大家介绍几种常用的配置方法。

Properties files

首先我们创建一个属性文件,内容如下:

userId=jianggujin
userName=\u848B\u56FA\u91D1
age=22
descript=${userId} ${userName} ${age}
   
   
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

示例代码:

package com.gujin.configuration;

import java.net.URL;
import java.util.Iterator;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;

public class ConfigurationTest
{
   @Test
   public void testProperties() throws Exception
   {
      Class<?> clazz = getClass();
      URL url = clazz.getResource("configuration.properties");
      PropertiesConfiguration configuration = new PropertiesConfiguration(url);
      // 遍历配置的键
      for (Iterator<String> iterator = configuration.getKeys(); iterator
            .hasNext();)
      {
         System.out.println(iterator.next());
      }
      System.out.println(configuration.getString("userId"));
      System.out.println(configuration.getInt("age"));
      System.out.println(configuration.getString("descript"));
   }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

运行结果:

userId
userName
age
descript
jianggujin
22
jianggujin 蒋固金 22
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

XML documents

同样的,我们先来创建一个xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Servlet</display-name>
  <servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>com.gujin.servlet.UrlPatternTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
     <filter-name>cd</filter-name>
     <filter-class>com.gujin.filter.CrossDomainFilter</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>cd</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

很熟悉有木有,这就是Java WEB项目的配置文件。

示例代码:

package com.gujin.configuration;

import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.junit.Test;

public class ConfigurationTest
{

   @Test
   public void testXML() throws Exception
   {
      Class<?> clazz = getClass();
      URL url = clazz.getResource("configuration.xml");
      XMLConfiguration configuration = new XMLConfiguration(url);
      System.out.println("RootElementName:"
            + configuration.getRootElementName());
      System.out.println("id:" + configuration.getProperty("[@id]"));
      System.out.println("version:" + configuration.getProperty("[@version]"));
      System.out.println("xmlns:xsi:"
            + configuration.getProperty("[@xmlns:xsi]"));
      System.out.println("xmlns:" + configuration.getProperty("[@xmlns]"));
      System.out.println("xsi:schemaLocation:"
            + configuration.getProperty("[@xsi:schemaLocation]"));
      System.out.println("display-name:"
            + configuration.getProperty("display-name"));

      // 解析servlet
      System.out.println("-----------------parse servlet");
      List<HierarchicalConfiguration> servlets = configuration
            .configurationsAt("servlet");
      for (HierarchicalConfiguration servlet : servlets)
      {
         System.out
               .println("servlet-name:" + servlet.getString("servlet-name"));
         System.out.println("servlet-class:"
               + servlet.getString("servlet-class"));
      }
      // 解析servlet mapping
      System.out.println("-----------------parse servlet mapping");
      List<HierarchicalConfiguration> servletMappings = configuration
            .configurationsAt("servlet-mapping");
      for (HierarchicalConfiguration servletMapping : servletMappings)
      {
         System.out.println("servlet-name:"
               + servletMapping.getString("servlet-name"));
         System.out.println("url-pattern:"
               + servletMapping.getString("url-pattern"));
      }
   }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

运行结果:

RootElementName:web-app
id:WebApp_ID
version:2.5
xmlns:xsi:http://www.w3.org/2001/XMLSchema-instance
xmlns:http://java.sun.com/xml/ns/javaee
xsi:schemaLocation:http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
display-name:Servlet
-----------------parse servlet
servlet-name:test
servlet-class:com.gujin.servlet.UrlPatternTest
-----------------parse servlet mapping
servlet-name:test
url-pattern:/
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值