How to Load Properties Using Apache Commons Configuration 2

This tutorial will show you how to load properties using Apache Commons 2. First, we’ll give you a simple example of how to load a single configuration or property file, and later on, we will show you how to load multiple configuration or property files in a single Configuration class.

Add Apache Commons Configuration 2 and Bean Utils

Firstly, add the dependency in your pom.xml if you are using Maven. You can get the latest version in this link. Apache Commons Configuration uses Apache Bean Utils so we will be also adding it in our dependencies.

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-configuration2</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

Load Properties using Apache Commons Configuration 2

For this example, we will be creating a file called application.properties. This file will hold the key-value pairs that we will be using in our program.

application.properties
project.title=How to Load Properties using Apache Commons
project.author=JavaPointers
project.site=https://javapointers.com

For us to load this file, we will be creating a singleton class that holds this configuration data. A singleton design pattern is best for this scenario as we only need to load/initialize the class once and thus, avoiding consuming resources unlike creating a new object every load.

package com.javapointers.properties;

import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;

public class SinglePropertyLoader {

    private static SinglePropertyLoader instance;
    private FileBasedConfiguration configuration;

    private SinglePropertyLoader() {
        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                        .configure(params.properties()
                                        .setFileName("application.properties"));
        try {
            configuration = builder.getConfiguration();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public static synchronized SinglePropertyLoader getInstance() {
        if (instance == null) {
            instance = new SinglePropertyLoader();
        }
        return instance;
    }

    public String getProperty(String key) {
        return (String) configuration.getProperty(key);
    }
}

In the code above, we defined a FileBasedConfiguration. We first created a Parameters class and set the filename. Then we passed it in the Builder and finally getting the configuration using the getConfiguration() method.

Below is our main method. Let’s run it and see the output.

    public static void main(String... args) {
        System.out.println(SinglePropertyLoader.getInstance().getProperty("project.title"));
        System.out.println(SinglePropertyLoader.getInstance().getProperty("project.site"));

    }

As a result, this will show the below output:

Loading Multiple Property files using Apache Commons Configuration 2

Earlier, we show you how to load a single property file. There will be scenarios where you have multiple configuration or property files that you want to load. We will show you how you can do it using CombinedConfiguration.

The CombinedConfiguration is already included in Apache Commons Configuration, and thus, we don’t need to add additional dependencies.

First, aside from application.properties that we have created earlier, we will add a new property file called system.properties.

Next, we will define a file named configuration.xml that contains the list of property files that we will combine.

system.properties
system.java.version=8
system.build.tool=maven
configuration.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<configuration>
    <properties fileName="application.properties"/>
    <properties fileName="system.properties"/>
</configuration>

Then, let’s create a new singleton class that holds these properties.

package com.javapointers.properties;

import org.apache.commons.configuration2.CombinedConfiguration;
import org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;

public class CombinedPropertyLoader {

    private static CombinedPropertyLoader instance;
    private CombinedConfiguration configuration;

    private CombinedPropertyLoader() {
        Parameters params = new Parameters();
        CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder()
                .configure(params.fileBased().setFileName("configuration.xml"));
        try {
            configuration = builder.getConfiguration();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public static synchronized CombinedPropertyLoader getInstance() {
        if (instance == null) {
            instance = new CombinedPropertyLoader();
        }
        return instance;
    }

    public String getProperty(String key) {
        return (String) configuration.getProperty(key);
    }
}

Let us now test our class. Here is our updated Main class.

    public static void main(String... args) {
        System.out.println(CombinedPropertyLoader.getInstance().getProperty("project.title"));
        System.out.println(CombinedPropertyLoader.getInstance().getProperty("project.site"));
        System.out.println(CombinedPropertyLoader.getInstance().getProperty("system.java.version"));
    }

Running the main method will show you the below output:

That’s it! You have learned how to load properties using Apache Commons Configuration 2.

Share this tutorial!