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 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里的“.”,轉換成了“/”,同上面的例子一樣。