@ConfigurationProperties in Spring Boot

The @ConfigurationProperties annotation in Spring Boot is used to map properties from external configuration files (such as application.properties or application.yml) to Java objects. This allows you to manage configuration in a type-safe manner and keep your configuration clean and maintainable.

In this tutorial, we'll explore how to use @ConfigurationProperties in a Spring Boot application.

Prerequisites

  • JDK 17 or later
  • Maven or Gradle
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Set Up a Spring Boot Project

1.1 Create a New Spring Boot Project

Use Spring Initializr to create a new project with the following dependencies:

  • Spring Web
  • Spring Boot Actuator
  • Spring Configuration Processor (optional, for IDE support)

Download and unzip the project, then open it in your IDE.

1.2 Configure application.properties

Set up the application properties for your project. We'll add some custom properties that we want to map to Java objects.

myapp.datasource.url=jdbc:mysql://localhost:3306/mydb
myapp.datasource.username=root
myapp.datasource.password=rootpassword
myapp.datasource.max-pool-size=10

myapp.mail.host=smtp.example.com
myapp.mail.port=587
myapp.mail.username=myemail@example.com
myapp.mail.password=secret

Explanation:

  • myapp.datasource.*: Custom properties for configuring the datasource.
  • myapp.mail.*: Custom properties for configuring the mail server.

Step 2: Create Configuration Properties Classes

We will create Java classes to map these properties.

2.1 Create a DataSourceProperties Class

Create a class to map the datasource properties.

package com.example.demo.config;

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

@Configuration
@ConfigurationProperties(prefix = "myapp.datasource")
public class DataSourceProperties {

    private String url;
    private String username;
    private String password;
    private int maxPoolSize;

    // Getters and setters
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMaxPoolSize() {
        return maxPoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }
}

Explanation:

  • @Configuration: Marks the class as a source of bean definitions.
  • @ConfigurationProperties(prefix = "myapp.datasource"): Maps properties prefixed with myapp.datasource to this class.
  • This class contains fields corresponding to the properties, along with getters and setters.

2.2 Create a MailProperties Class

Create a class to map the mail properties.

package com.example.demo.config;

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

@Configuration
@ConfigurationProperties(prefix = "myapp.mail")
public class MailProperties {

    private String host;
    private int port;
    private String username;
    private String password;

    // Getters and setters
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Explanation:

  • @Configuration: Marks the class as a source of bean definitions.
  • @ConfigurationProperties(prefix = "myapp.mail"): Maps properties prefixed with myapp.mail to this class.
  • This class contains fields corresponding to the properties, along with getters and setters.

Step 3: Enable Configuration Properties

3.1 Create a Configuration Class

You need to create a configuration class to enable your @ConfigurationProperties beans.

package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@Configuration
@EnableConfigurationProperties({DataSourceProperties.class, MailProperties.class})
public class AppConfig {
}

Explanation:

  • @Configuration: Marks the class as a source of bean definitions.
  • @EnableConfigurationProperties: Enables support for @ConfigurationProperties-annotated beans.

Step 4: Use Configuration Properties in Your Application

4.1 Create a REST Controller

Create a REST controller to demonstrate how to use the configuration properties.

package com.example.demo.controller;

import com.example.demo.config.DataSourceProperties;
import com.example.demo.config.MailProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Autowired
    private MailProperties mailProperties;

    @GetMapping("/config/datasource")
    public DataSourceProperties getDataSourceConfig() {
        return dataSourceProperties;
    }

    @GetMapping("/config/mail")
    public MailProperties getMailConfig() {
        return mailProperties;
    }
}

Explanation:

  • @RestController: Marks the class as a REST controller.
  • @Autowired: Injects the DataSourceProperties and MailProperties beans.
  • The getDataSourceConfig and getMailConfig methods return the configuration properties as JSON responses.

4.2 Add Main Application Class

Ensure your main application class is present.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Step 5: Testing the Application

5.1 Run the Application

Run the Spring Boot application and navigate to the following URLs to see the loaded configuration properties:

  • http://localhost:8080/config/datasource
  • http://localhost:8080/config/mail

Conclusion

In this tutorial, you have learned how to use the @ConfigurationProperties annotation in a Spring Boot application to map external configuration properties to Java objects. This approach provides a clean and type-safe way to manage configuration properties and keeps your code organized and maintainable.

Comments