Apache Commons Configuration工具类简介说明

书欣 Java经验 发布时间:2022-08-09 16:41:25 阅读数:18290 1
下文笔者讲述Apache Commons Configuration工具类简介说明,如下所示
Apache Commons Configuration用于应用程序的配置管理工具
 可从properties或xml文件中加载配置信息
例:
package com.java265;

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

Apahce Commons Configuration对XML documents读取的方法

例:
package com.java265;

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"));
}
}
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202208/16600345044199.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者