Jakarta commons configuration Study Notes
Keywords: Jakarta, commons, Configuration
Runtime Dependencies
Some components required for running:
- Commons beanutils
- Commons collections
- Commons Digester
- Commons Lang
- Commons Logging
- XML APIs or xerces
Dependency:
- Core
- Commons collections
- Commons Lang
- Configurationfactory
- Commons Logging
- Commons Digester
- Databaseconfiguration
- Xmlconfiguration, hierarchicalxml+aion
- Jndiconfiguration
- Configurationdynabean
- Commons beanutils
- Commons Logging
Use configurationfactory for configuration
Portal/
| --Src/
| --WEB-INF/
| ---- Config. xml
| ----Properties/
| ------ Usergui. Properties
| ----Classes/
| ------ Configurationfactorytest. Class
Configurationfactory provides two methods to specify the location of the configuration file:
- Setconfigurationurl (URL)
- Setconfigurationfilename (string)
Use the factory. setconfigurationurl (URL) method to specify the configuration file configurationfactory factory = new configurationfactory ();
URL url = new file ("WEB-INF/config. xml"). tourl ();
Factory. setconfigurationurl (URL );
Configuration Config = factory. getconfiguration ();
System. Out. println (config. getstring ("Colors. Background "));
In this case, the declaration of other configuration files in config. xml should be relative to the path of config. xml. <? XML version = "1.0" encoding = "ISO-8859-1"?>
<Configuration>
<PropertiesFilename = "properties/usergui. properties"/>
<! -- OrFilename = "./properties/usergui. properties"-->
</Configuration>
Use the factory. setconfigurationfilename (string) method to specify the configurationfactory factory = new configurationfactory ();
Factory. setconfigurationfilename ("WEB-INF/config. xml ");
Configuration Config = factory. getconfiguration ();
System. Out. println (config. getstring ("Colors. Background "));
In this case, the declaration of other configuration files in config. xml should be relative to the root directory path of the project: <? XML version = "1.0" encoding = "ISO-8859-1"?>
<Configuration>
<PropertiesFilename = "WEB-INF/properties/usergui. properties"/>
</Configuration>
! Note: If the path of the configuration file is incorrect, the following two exceptions will occur:
- org. Apache. commons. configuration. configurationfactory. getconfiguration (configurationfactory. Java: 135)-exception caught opening stream to URL indicates that config. xml cannot be found
- Org. apache. commons. digester. digester. endelement (digester. java: 1069)-end event threw exception Java. lang. reflect. invocationtargetexception indicates config cannot be found. other configuration files declared in XML