Reading Property File in Java using Apache Commons Configuration
This tutorial demonstrates reading property file in Java using Apache Commons Configuration library.
You may have different configuration sources such as property files, XML files, system properties, environment variables etc. Apache Commons Configuration allows you to collect properties from many such sources and access them as a single configuration object. It also allows you to set the order of preference among the property sources to define an override mechanism.
Apache Commons Configuration provides a CompositeConfiguration class which represents a unified property bag to which you can attach different configuration sources. The properties of configuration sources that are added first hide the property values of configuration sources added later.
In the example below, we define two property sources namely Java system properties and a property file. A sample property file with a couple of sample properties is shown below. The idea is to specify the configuration properties in a property file and be able to override one or more of those properties from command line through java system properties.
Sample Property File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
threadPool.size = 10 log.level = error server.host = localhost server.port = 40000 socket.connectTimeout = 20000 socket.readTimeout = 20000 mail.sender.email = xyz@gmail.com mail.sender.password = xYqs#1w mail.subject.default = Notification from Expresso mail.smtp.host = smtp.gmail.com mail.smtp.port = 465 |
Java Code
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 |
package com.wilddiary.examples.apachecommons.configuration; import java.util.Iterator; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.SystemConfiguration; public class ExampleCompositeConfiguration { public static void main(String[] args) throws Exception { CompositeConfiguration config = new CompositeConfiguration(); // add config sources. // add SystemConfiguration first below we need to override properties // using java system properties config.addConfiguration(new SystemConfiguration()); config.addConfiguration(new PropertiesConfiguration("/Users/userXYZ/config.properties")); System.out.println("----------------------------"); System.out.println("Listing composite properties"); System.out.println("----------------------------"); Iterator<String> keys = config.getKeys(); while (keys.hasNext()) { String key = keys.next(); System.out.println(key + " = " + config.getProperty(key)); } } // main() ends } |
Running the Java code
1 |
java –classpath … com.wilddiary.examples.apachecommons.configuration.ExampleCompositeConfiguration |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
---------------------------- Listing composite properties ---------------------------- java.vm.specification.version = 1.7 user.name = userXYZ java.vm.version = 24.51-b03 java.specification.version = 1.7 java.vm.vendor = Oracle Corporation sun.arch.data.model = 64 user.home = /Users/userXYZ threadPool.size = 10 log.level = debug server.host = localhost server.port = 40000 socket.connectTimeout = 20000 socket.readTimeout = 20000 mail.sender.email = xyz@gmail.com mail.sender.password = xYqs#1w mail.subject.default = Notification from Expresso mail.smtp.host = smtp.gmail.com mail.smtp.port = 465 |
Running the Java code with overridden values
We will now override properties – server.port and log.level, by specifying them as system properties with new values.
1 |
java –classpath … -Dserver.port=12345 -Dlog.level=debug com.wilddiary.examples.apachecommons.configuration.ExampleCompositeConfiguration |
Output
Notice how the overridden values mask the values from the property file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
java.vm.specification.version = 1.7 log.level = debug user.name = userXYZ server.port = 12345 java.vm.version = 24.51-b03 java.specification.version = 1.7 java.vm.vendor = Oracle Corporation sun.arch.data.model = 64 user.home = /Users/userXYZ threadPool.size = 10 server.host = localhost socket.connectTimeout = 20000 socket.readTimeout = 20000 mail.sender.email = xyz@gmail.com mail.sender.password = xYqs#1w mail.subject.default = Notification from Expresso mail.smtp.host = smtp.gmail.com mail.smtp.port = 465 |