Spring Boot Configuration Properties

@ConfigurationProperties is a feature in Spring Boot that allows you to bind external configurations to a Java class.

It maps property names to fields in a Java class, making it easy to manage external configurations.

So, we can use this Spring functionality if we want to set the properties into a Java class.

Dependencies

You’ll need at leas this dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

How to use Configuration Properties with a Java Pojo class

The properties can be specified from your application.properties files.

The annotated class can then be used to access the properties in your application code. This helps to separate the configuration from the implementation, making it easier to manage and maintain.

Here is a simple example of how you could use @ConfigurationProperties in a Spring Boot application:

If we have this property file:


app.name=The name of this app
app.description=This description of this app

We can set these properties in a Java class automatically. For that, you have to annotate the Java class with these two annotations:

  • @ConfigurationProperties
  • @Component

The prefix in ConfigurationProperties has to be equal to the prefix of the property.

@ConfigurationProperties(prefix = "app")
@Component
public class AppInfo {

    private String name;
    private String description;

    // set and get.. 
    
    @Override
    public String toString() {
        return "AppInfo{" +
                "name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

}

Note how the property prefix is joined with the annotation and each property with the class attribute.

Spring Boot Configuration Properties

How to use Configuration Properties with a Spring Bean

This way is similar to the previous one we learned.

For example, for these properties:

app.mail.hostname=host@mail.com
app.mail.port=9000
app.mail.from=mailer@mail.com

Let’s define the class AppConfig and annotate it with @Configuration. The class AppConfig is a class of @Configuration that defines a component with the annotation @Bean.

The mailData() * method returns an instance of MailData and annotates with @ConfigurationProperties passing as parameter the prefix “app.mail” to specify that its attributes (those of the MailData class) must be filled with properties that have the prefix “app.mail”.

@Configuration
public class AppConfig {

    @Bean
    @ConfigurationProperties(prefix = "app.mail")
    public MailData mailData() {
        MailData mailData = new MailData();
        return mailData;
    }

}

Running both examples

You can then use the bean or the component in other parts of your application through dependency injection, for example:

@SpringBootApplication
public class SpringBootConfigurationPropertiesApplication {

    @Autowired
    AppInfo appInfo;

    @Autowired
    MailData mailData;

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

    @PostConstruct
    public void post() {
        System.out.println(appInfo);
        System.out.println(mailData);
    }
}

The output of that is:

AppInfo{name='The name of this app', description='This description of this app'}
MailData{hostName='host@mail.com', port=9000, from='mailer@mail.com'}

Conclusion:

@ConfigurationProperties is a Spring annotation used to map external configuration properties to fields in a Java class, making it easy to get property values in your services.

This code is in GitHub

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)

See also