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.
Custom configuration properties in 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.
app.mail like app.mail.host and app.mail.port.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; } }
MailProperties class.application.properties
app.mail.host=smtp.example.com
app.mail.port=587import 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 }
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
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(); } }
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.
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.