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
示例代码:
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
运行结果:
userId
userName
age
descript
jianggujin
22
jianggujin 蒋固金 22
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
很熟悉有木有,这就是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
运行结果:
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:/
————————————————
版权声明:本文为CSDN博主「蒋固金」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jianggujin/article/details/51167495