使用 Configuration

类别:Java 点击:0 评论:0 推荐:
原文地址:http://jakarta.apache.org/commons/configuration/overview.html 使用 Configuration

看下面的例子可以尽快学会怎样使用Configuration ,接下来开始示范怎样从多根不同来源获取配置。

Configuration Sources配置文件

Currently there are quite a number of different sources of Configuration objects. But,

现在有很多种配置信息的来源,通过象 XMLConfiguration 或者JNDIConfiguration的类型来使用一个Configuration 对象,获得配置信息的底层细节是透明的. 这些配置信息的来源有:   PropertiesConfiguration 从 properties文件中加载配置信息。. BaseConfiguration 直接在内存中生成配置信息的方法。 XMLConfiguration 从xml文件中获取配置信息。. JNDIConfiguration 通过JNDI树来使用属性关键词,可以当作配置属性值。 ConfigurationConverter 读取java.util.Properties 或者o.a.c.collections.ExtendedProperties 并把它们转换成一个 Configuration 对象.

 混合的配置源

你常常需要提供一个配置信息的基本设置,并且允许用户根据自己的特定环境很方便的修改它们。一种方法是在代码中对默认值使用硬编码写死,同时提供一个property文件来来覆盖默认值。这是一种很死板的做法. 取而代之的是, 通过CompositeConfiguration 你可以提供多种设置配置信息的途径。 你可以手工来实现(请参考JUnit testcase "TestCompositeConfiguration.java),也可以借助ConfigurationFactory 来完成。

通过 ConfigurationFactory, (请参考 the Junit testcase "TestConfigurationFactory.java") 加载一个可以区别各种 Configuration 对象的摘要xml文件. 下面是一个配置文件示例digesterRules.xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?> <configuration> <jndi className="org.apache.commons.configuration.JNDIConfiguration" prefix="java:comp/env"/> <properties className="org.apache.commons.configuration.PropertiesConfiguration" fileName="conf/test.properties"/> <xml className="org.apache.commons.configuration.XMLConfiguration" fileName="conf/test.xml"/> </configuration> 这个文件声明了需要加载所有在java:comp/env下的JNDI键值对,还有一个properties文件conf/test.properties ,以及一个xml文件conf/test.xml。请阅读测试用例和conf/ 目录下的文件来获得更多的关于怎么构造配置文件的信息。

加载配置的优先级是从第一个配置开始直到最后一个。因此在上面这个例子中.假设有一个叫 "test.precendence "的JNDI 键值对  ,在xml文件里也有一个叫 "test.precendence ",的键值对那么来自 JNDI的对应值被优先返回,而不会是在xml文件里的这个值。 这样就允许你覆盖通过在一个 properties/xml file,中设置默认值,而通过JNDI或者另外的XML/properties文件来覆盖默认值!!!

 

配置细节

Configuration is done by taking the configuration XML file and using included Digester rules, parsing the individual configurations. Make sure to include the various dependencies required for each type of configuration!

典型的  Properties 文件       
  <properties className="org.apache.commons.configuration.PropertiesConfiguration" fileName="conf/test.properties"/>
  
       

这个配置描述很简单,你只需要说明property文件的路径即可. 

XML属性文件

<xml className="org.apache.commons.configuration.XMLConfiguration" fileName="conf/test.xml"/>

这个配置和典型的 properties 文件很相似. 然而, xml文件必须满足特定的格式,当前xml文件没有DTD.约束。

<baseElement> <element>value</element> <element2> <subelement> <subsubelement>I'm complex!</subsubelement> </subelement> </element2> <test> <short>8</short> </test> </baseElement>

在上面的例子中,根元素是被忽略掉的。 因此要得到“8”这个值,你应该从配置中通过键值对“test.short”来获取,根元素可以使用任意值。

JNDI Properties File

<jndi className="org.apache.commons.configuration.JNDIConfiguration" prefix="java:comp/env"/>

在例如设置邮件服务器这样的特定环境使用这个配置非常有用! 下面的前缀告诉了ConfigurationFactory 获取你的设置的根节点。      
    <env-entry>
        <env-entry-name>smtp</env-entry-name>
        <env-entry-value>127.0.0.1</env-entry-value>
        <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
   
    <env-entry>
        <env-entry-name>test/short</env-entry-name>
        <env-entry-value>80</env-entry-value>
        <env-entry-type>java.lang.Short</env-entry-type>
    </env-entry>

  
       

注意! 如果你有一个叫"test.short"的属性,如果使用了空格在里面,将被转换成 键值对"test/short". 因此,你不能给在来自JNDI的属性名字里使用空格!  如果你需要使用到这样的键值对, 请确信你把web.xml里的“.”,转换成了“/”,同上面的例子一样。

 

本文地址:http://com.8s8s.com/it/it11479.htm