commons-configuration使用介绍

薛弘济
2023-12-01

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}

示例代码:

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"));
   }
}

运行结果:

userId
userName
age
descript
jianggujin
22
jianggujin 蒋固金 22

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>

很熟悉有木有,这就是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"));
      }
   }
}

运行结果:

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:/
 类似资料: