Spring Boot has an awesome feature named @ConfigurationProperties that lets you automatically bind the properties defined in the application.properties file into a Java Bean class. Let's see how to do it.

Consider the following class:

package com.example;

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

@ConfigurationProperties(prefix = "jdbc")
public class DataSourceConfig
{
    private String driver;
    private String url;
    private String username;
    private String password;
    //setters and getters
}

The @ConfigurationProperties(prefix = "jdbc") annotation does the job to bind all the properties with prefix "jdbc" to the corresponding fields of the POJO class.

If you define additional file properties in future, you may simply add a corresponding field in the above class, and spring boot will automatically bind the field with the property value.

Here is an example of application.properties that sets the property to be injected in the Bean:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo
jdbc.username=root
jdbc.password=secret

Enabling Configuration Properties

In order to enable the ConfigurationProperties feature, you need to add @EnableConfigurationProperties annotation to any configuration class. So for example:

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties({
        DataSourceConfig.class
})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Relaxed Binding

Spring Boot is not too strict on the binding. The bean property names need not be exactly the same as the property key names. Spring Boot supports relaxed binding. So for example, bean property driverClassName will be mapped from any of these: driverClassName, driver-class-name, or DRIVER_CLASS_NAME.

Injecting Configuration Properties

The @ConfigurationProperties classes are not different from other Spring Beans, therefore you can inject them with the @Autowired annotation:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ExampleController {

    @Autowired
    private DataSourceConfig properties;

    
}

Validating Properties with the Bean Validation API

You can use Bean Validation API annotations such as @NotNull,@Min, @Max, etc., to validate the property’s values. For example:

@Component
@ConfigurationProperties(prefix="props")

public class MyProperties {

@NotNull
private String applicationName;

 

@NotNull
@Email
private String email;

 

@Min(1) @Max(5)
private Integer severity;

//setters and getters

}

If you configured any property values that are invalid as per the configured validation annotations, an exception will thrown at the application startup time. Enjoy the awesome power of @ConfigurationProperties in Spring Boot!

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials