How to load properties using Spring Boot @ConfigurationProperties

Spring Boot has a very elegant way to load properties, which we can provide the standard way as in –

application.properties

or in a hierarchical yml format as –

application.yml

Its perfectly fine to move on with the approach of your choice.


Below snapshot depicts the Spring Boot project structure with property files in place –

Spring Boot Project Structure

@ConfigurationProperties annotation provides the capability to map the property file entries into corresponding object while also supporting the hierarchical YML structure. For example to bind the above mentioned properties, I would write a new class as –

The property file will bind to the above class, so we can easily retrieve the property values in our application by injecting the instance of the above class using @Autowired annotation. For example –

For the demo purpose, we are setting the list and map values into the model object, just to demonstrate that everything explained above works perfectly.

welcome.html

When we run this Spring Boot application, what output you see would just be what you expected.

Spring Boot Property File Output

Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.