0
0
Spring Bootframework~5 mins

Custom configuration properties in Spring Boot

Choose your learning style9 modes available
Introduction

Custom configuration properties let you organize your app settings in a neat way. They help keep your code clean and easy to change without touching the code itself.

You want to group related settings like database info or API keys in one place.
You need to change app behavior without rebuilding the app.
You want to avoid hardcoding values inside your Java classes.
You want to validate configuration values automatically.
You want to keep configuration readable and maintainable.
Syntax
Spring Boot
1. Create a POJO class with fields for your properties.
2. Annotate the class with @ConfigurationProperties(prefix = "your.prefix")
3. Add @Component or @EnableConfigurationProperties to register it.
4. Use getters and setters for the fields.
5. Define the properties in application.properties or application.yml using the prefix.

The prefix groups your properties under a common name.

Spring Boot automatically binds the properties to your class fields.

Examples
This class binds properties starting with app.mail like app.mail.host and app.mail.port.
Spring Boot
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app.mail")
public class MailProperties {
    private String host;
    private int port;

    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;
    }
}
These are the property values that will be injected into the MailProperties class.
Spring Boot
application.properties
app.mail.host=smtp.example.com
app.mail.port=587
Alternative way to register your properties class without using @Component.
Spring Boot
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class AppConfig {
    // This enables the binding if you don't use @Component on the properties class
}
Sample Program

This Spring Boot app defines a custom properties class MailProperties bound to app.mail prefix. The controller shows these values at /mailconfig.

Set these in application.properties:

app.mail.host=smtp.example.com
app.mail.port=587
Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Component
@ConfigurationProperties(prefix = "app.mail")
class MailProperties {
    private String host;
    private int port;

    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;
    }
}

@RestController
class MailController {
    private final MailProperties mailProperties;

    public MailController(MailProperties mailProperties) {
        this.mailProperties = mailProperties;
    }

    @GetMapping("/mailconfig")
    public String getMailConfig() {
        return "Mail Host: " + mailProperties.getHost() + ", Port: " + mailProperties.getPort();
    }
}
OutputSuccess
Important Notes

Make sure to add @Component or use @EnableConfigurationProperties to register your properties class.

Use camelCase in Java fields and kebab-case or dot notation in properties files.

You can add validation annotations like @NotNull to check values automatically.

Summary

Custom configuration properties help organize app settings cleanly.

Use @ConfigurationProperties with a prefix to bind properties to a class.

Define values in application.properties or application.yml and inject the class where needed.