Binding properties in Spring Boot with @ConfigurationProperties annotation

I introduced you to the @Value annotation to ingest the values of properties located in the properties files with the Spring framework. Of course, we can also use this @Value annotation in Spring Boot. However, in Spring Boot, we also have another annotation, @ConfigurationProperties, to do this. What is the difference between 2 annotations and why are they creating another new annotation to ingest the value of the property in the properties file? Let’s find out in this tutorial.

First, I will create a Spring Boot project as follows:

Binding properties in Spring Boot with @ConfigurationProperties annotation

In particular, the SpringBootConfigurationPropertiesApplication class implements the CommandLineRunner interface to run the Java console, with the following content:

Class Student will contain student information with some attributes as follows:

The student.properties file contains student information with the following content:

Using @Value annotation, we can ingest the value of the properties in the student.properties file into the Student object.

then display this information to the console as follows:

Result:

Binding properties in Spring Boot with @ConfigurationProperties annotation

Now, If you use the @ConfigurationProperties annotation in the Student class by declaring the following:

running again, the results are still the same.

As you can see, with the use of the @ConfigurationProperties annotation, you do not need to declare which property of the Student object will be ingested to the value of the property in the student.properties file. Everything is completely automatic.

To do this, of course, we have to follow some rules 🙂

In the above example, “student” is the prefix that the @ConfigurationProperties annotation will use to automatically ingest properties in the student.properties file. The properties start as “student” and the rest that coincides with the name of the Student object will be ingested to that property of the Student object. Here, the rest of these properties will not distinguish between uppper case, lower case, underlines or dashes.

For example, you can declare the student.properties file as follows:

the results are still the same. This is called relaxed binding. 🙂

Of course, if our properties do not start with the student:

then you do not need to declare the prefix in the @ConfigurationProperties annotation anymore:

In case the Student object contains a collection or array, for example:

then you can declare the following in the student.properties file to Spring Boot automatically ingest the value of the variable scores on the Student object:

Then the results will be:

Binding properties in Spring Boot with @ConfigurationProperties annotation

In case the Student object contains another object, such as:

then with the following declaration in the properties file:

The information of the Address object is also ingested as normal:

Binding properties in Spring Boot with @ConfigurationProperties annotation

Add Comment