@ConfigurationProperties : Spring Boot’s way of handling properties

Hello coders,


We are always in quest of improving something or the other in our lifetime, this urge also fits in for technologies as well. We can see many enhancements and updates on each and every technology so as in our favourite Spring Framework.

Spring has come a long way ahead since its inceptions.. Yes, the latest buzz word is “SPRING BOOT”!! Though it has made a lot of things easier for a developer, but sometime it debatable. In a nutshell, understanding it deeply is a must.

Here, I will be sharing a tiny, may be a tiniest of a configuration annotations @ConfigurationProperties 

We must have used properties files in almost all of our applications. Spring Boot provides a clean and neat way of using it in applications. Instead of writing the long properties name which is very error-prone (we must have spent lot of time debugging and at last it just a spelling mistake of property name).

So, how about creating a class that gets mapped to properties on a file? Isn’t it a neat style?

Let’s get into coding. We used to start with writing the properties first, but here we can start designing a properties in a Class then move to properties file.

Create a class:

package com.pack.subpack.utils.properties;

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

public class JwtProperties {

    private String header;
    private String secret;
    private int expiration;
    private Map <String,String> authorisation;
    private List<String> someProperties;

//... Getters & Setters

This class is marked as @Component. Which makes it a candidate for @Autowiring. Also we have used @ConfigurationProperties(prefix=”jwt”), which indicates that this will get mapped to all properties starts with jwt. Lets have an look at the properties file (properties.yaml)

  expiration: 604800
  path: auth
  secret: secret
  username: sovanm
  salt: mysecr3t
    - prop1
    - prop2

Now the mapping is done, but how to use it? This must be simple. Yes you guessed it right. It is simple. Just Inject the Component and enjoy the get methods to get the values.

private JwtProperties jwtProperties;
// Constructor Injection
public JwtTokenUtils(JwtProperties jwtProperties) {
    this.jwtProperties = jwtProperties;
    System.out.println("Secret: " + this.jwtProperties.getSecret());
    System.out.println("SomePropertiesList: " +
        .forEach(e-> {
             System.out.println(e.getKey() + " : " + e.getValue());

Done! we can now use the properties anywhere in this class. It helps to make our work easier as we all love to use object intelligence rather than writing the string version as

private Map authorisations;

Features, that we can take advantages are:

  •  We can use JSR-303 Annotations for validations. (E,g: Used above)
  •  Intelligence available in the properties file.
  • We can load family of properties into separate classes using the appropriate prefix.

There are lots to know..but  I hope, this might help you getting started dealing with properties file in Boot’s way.

Thanks for reading and Happy Coding 🙂



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s