Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Methods to read Spring Configuration

In this tutorial, we will learn the ways to read the spring boot configuration. We have learnt that spring boot auto-configures the project on the basis of dependencies selected at the time of project set up. Another way of writing custom configuration is the use of application.properties file where user properties can be defined.

We will write some custom properties in application.properties and will create a Component to read the details in the application.

Let us add the below properties in the application.properties file.

custom.property.security=enabled
custom.property.validation=enabled
custom.property.spring=boot

Create a class under root package(com.tutorial.studytonight) as follows:

@Component
@ConfigurationProperties("custom.property")
public class ApplicationConfiguration {

    private String security;
    private String validation;
    private String spring;

    //Setters and Getters

}

The class is annotated with @Component and @ConfigurationProperties annotations. @Component annotation is used to mark a class not a method whereas @Bean is used for the method. The above class created will be managed by spring as a Component. We have discussed about @ComponentScan which is used to scan all the components available. It can be replaced with @SpringBootApplication as it can also perform the @ComponentScan functionality. @ConfigurationProperties is provided with a string whose format is similar to the properties mentioned in application.properties and the last prefix of those properties are declared as fields in the above class(compare the application.properties and the ApplicationConfiguration class).

We will now develop a small API that allow us to display the configuration using the above @Component annotated class.

Spring boot allows us to autowire the ApplicationConfiguration class and observe the below code for the API.

public class ResourceController {
    @Autowired
    ApplicationConfiguration applicationConfiguration;
    
    @GetMapping("/getConfiguration")
    public ApplicationConfiguration getConfiguration(){
        return applicationConfiguration;
    }
}

The above class returns the reference of ApplicationConfiguration which was autowired by spring boot. As the request is a GET request, let us hit the API from browser:

ApplicationConfiguration fields with the mentioned configuration has been returned to the browser.

Using Annotation to get Application Configuration:

Another way to read the configuration is to use @Value annotation. @Value reads the configuration on the basis of the string passed, observe the below code:

public class ResourceController{ 

    @Value("${custom.property.spring}")
    String val;

        @GetMapping("/getConfigurationProperty")
        public String getConfigurationProperty() {
            return val;
        }
}  

The above code reads the specified custom.property.spring property and binds the value to a String variable.

The above code has a GET request handler which returns the value to client.

It is useful in cases when the application needs some initialization parameters such as the security token validation time etc. For example, let us assume that a user sends a JWT token and it should be valid for 24 hours. We can set the property in our application.properties file and can be used whenever the user tries to authenticate with the JWT token. The spring-security module provides a way to handle JWT. When microservices architecture is followed, it is very important to deal with the configuration management.

Conclusion: In this tutorial we have seen the custom configuration defined by the user in application.properties and the methods to read the application configurations by using the ConfigurationProperties class and the @Value annotation.