Working with .properties files using Apache Common Configuration

In the previous tutorial, I introduced you how to work with .properties files using the Properties class. There are other needs related to configuration information of applications that this Properties class does not support, for example:

Reusing properties that have been declared in other properties is not supported.

Suppose I have the following properties:

Because these properties have the same value at the beginning, I will need to reuse the property “huongdanjava.base.url” in other properties using variable interpolation with the syntax “${<property_name>}” as follows :

So when I change the base url information, I just need to change the property “huongdanjava.base.url”.

Get configuration information from environment variables
When running an application, sometimes we will need to get the value of an environment variable to inject into the application’s configuration information. As in the example above, if the value of the property “huongdanjava.base.url” is not configured directly in the .properties file but will be taken from the environment variable “HUONGDANJAVA_BASE_URL”, what will I do? By default, the Properties class is not supported.

All of the above problems can be easily solved if you use the Apache Common Configuration.

How is it in detail? We will find out together in this tutorial!

I will create a Maven project as an example:

with Apache Common Configuration dependency is declared as follows:

I will read the file located in the src/main/resources folder with the following content:

If you use the Properties class, you will see the following results when reading the property “”:

The value of the “huongdanjava.base.url” property has not been replaced in the value string of the “” property as you can see!

Now, we will use Apache Common Configuration to solve this problem!

In Apache Common Configuration, the Configuration interface helps us work with all types of configurations that our application needs. For the .properties file, we will use the implementation of this Configuration interface, the PropertiesConfiguration class.

To read the contents of the .properties file with Apache Common Configuration, we will use the FileBasedConfigurationBuilder class with the PropertiesConfiguration class as the parameter as follows:

You need to specify the .properties file to read using the configure() method as follows:

Now we can get the object of the Configuration class from the above class builder:

Các bạn có thể lấy đối tượng của class Properties từ đối tượng Configuration này như sau:

Now we can get the information in the .properties file using this Properties object.

You will see the following results:

when retrieving the value of property “”:

So the value of the “huongdanjava.base.url” property has been replaced in the value of the “” property!

To get the configuration information configured in the environment variable, you can declare the environment variable using variable interpolation starting with the keyword “env:” as follows:

At this point, if we get the value of the property “huongdanjava.introduction.url”:

You will see the results we want as follows:

with the HUONGDANJAVA BASE_URL environment variable configured when running the example application as follows:

So, with Apache Common Configuration, we have solved the limitations of Java’s Properties class!

Add Comment