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.
Practice
@ConfigurationProperties in Spring Boot?Solution
Step 1: Understand the role of
This annotation is used to map external configuration values (from files like@ConfigurationPropertiesapplication.propertiesorapplication.yml) to fields in a Java class.Step 2: Compare with other options
Options A, C, and D relate to other Spring Boot features but not to property binding.Final Answer:
To bind external configuration properties to a Java class -> Option DQuick Check:
Property binding = B [OK]
- Confusing @ConfigurationProperties with @RestController
- Thinking it manages database connections
- Assuming it enables security automatically
@ConfigurationProperties support in a Spring Boot application?Solution
Step 1: Identify how to activate configuration properties
Spring Boot requires@EnableConfigurationPropertieson a configuration or main class to enable binding of@ConfigurationPropertiesclasses.Step 2: Review other annotations
@ComponentScanscans components but does not enable properties binding.@RestControlleris for web controllers.@SpringBootApplicationis for main app class but does not alone enable properties binding.Final Answer:
Add @EnableConfigurationProperties on a configuration class or main class -> Option BQuick Check:
Enable config properties = D [OK]
- Confusing @ComponentScan with enabling properties
- Adding @RestController on properties class
- Assuming @SpringBootApplication alone enables properties
application.yml and class, what will be the output of System.out.println(appProperties.getTimeout())?
application.yml:
app:
timeout: 5000
Java class:
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private int timeout;
public int getTimeout() { return timeout; }
public void setTimeout(int timeout) { this.timeout = timeout; }
}Solution
Step 1: Understand property binding from YAML to class
Theapp.timeoutproperty in YAML is set to 5000. The class with@ConfigurationProperties(prefix = "app")binds this value to thetimeoutfield.Step 2: Check getter method output
CallinggetTimeout()returns the bound value 5000, so the printed output is 5000.Final Answer:
5000 -> Option CQuick Check:
Bound value printed = 5000 [OK]
- Forgetting to add setter method causes binding failure
- Using wrong prefix in annotation
- Expecting null or default instead of bound value
@ConfigurationProperties(prefix = "server")
public class ServerProperties {
private String port;
public String getPort() { return port; }
// Missing setter method
}Solution
Step 1: Check property binding requirements
Spring Boot binds properties using setters. Without a setter for 'port', the value cannot be set.Step 2: Validate other options
The prefix 'server' is valid. The class does not require @Component if registered via @EnableConfigurationProperties. Fields should be private with getters/setters, not public.Final Answer:
Missing setter method for 'port' prevents binding -> Option AQuick Check:
Setter missing = binding fails [OK]
- Assuming prefix must be custom or different
- Thinking @Component is always required
- Making fields public instead of using setters
application.properties like this:
mail.server.host=smtp.example.com mail.server.port=587Which of the following Java classes correctly models this nested property using
@ConfigurationProperties?Solution
Step 1: Understand nested property binding
Properties likemail.server.hostrequire a class with prefixmailand a nested classServerto holdhostandport.Step 2: Analyze options
@ConfigurationProperties(prefix = "mail") public class MailProperties { private Server server; public static class Server { private String host; private int port; // getters and setters } // getters and setters for server } correctly uses@ConfigurationProperties(prefix = "mail")with a nestedServerclass. @ConfigurationProperties(prefix = "mail.server") public class MailProperties { private String host; private int port; // getters and setters } flattens properties incorrectly. @ConfigurationProperties(prefix = "mail") public class MailProperties { private String host; private int port; // getters and setters } misses nesting. @ConfigurationProperties(prefix = "mail.server") public class MailProperties { private Server server; public static class Server { private String host; private int port; // getters and setters } // getters and setters for server } uses prefixmail.serverbut still nests, which is inconsistent.Final Answer:
@ConfigurationProperties(prefix="mail") with nested Server class -> Option AQuick Check:
Nested class with prefix mail = A [OK]
- Using wrong prefix for nested properties
- Flattening nested properties into one class
- Missing getters/setters in nested class
Ready to prove it?
Pass the test and this pattern moves unknown
