This week, i have explored the apache
commons-configuration third part jar. It sounds more interesting to read the configuration
details from multiple properties files in our project at once.
Sample Configuration file:
configuration.xml – File to configure the
list of properties file to be loaded from your application.
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<system />
<properties
fileName="logging.properties" />
<properties fileName="appConfig.properties"
/>
<properties fileName="webserviceEndPoints.properties"
/>
<properties fileName="message.properties"
/>
<properties
fileName="com/x/y/z/a.properties"/>
<properties
fileName="com/x/y/z/b.properties"/>
</configuration>
/**
* This class is to read values from the properties files listed in central
configuration file. Need to define the *all configuration/properties file in
the configuation.xml
*/
public final class ConfigurationManager {
private static final String
CONFIG_FILE = "configuration.xml";
private static Configuration
configuration;
private ConfigurationManager() {
final URL configFileURL =
Thread.currentThread().getContextClassLoader().getResource(CONFIG_FILE);
final
DefaultConfigurationBuilder builder = new
DefaultConfigurationBuilder(configFileURL);
configuration =
builder.getConfiguration(true);
}
Now, how do we get the value from property file using key?
Each
method returns an object by looking up in the property file associated with the
key if the corresponding key is missing it will return defaultValue which is
the argument.
configuration.getInteger(Key)
configuration.getInteger(Key,
defaultValue) - If not present in prop file
.getString
.getBoolean
.getDouble
.getStringArray
.getList etc.
You can have to
explore for additional methods.
Reference:
commons-configuration-1.5.jar. Please refer the features
here for configuration (http://commons.apache.org/configuration)
No comments:
Post a Comment