Spring Boot Configuration Properties
Real applications carry dozens of settings: mail servers, retry limits, file folders, feature switches, and partner addresses. Reading each one with a separate @Value annotation scatters keys across the code. The @ConfigurationProperties annotation gathers related settings into one typed Java object that is easy to read, validate, and test.
Spring Boot binds the values from application.properties or application.yml into that object automatically.
The Labeled Toolbox Idea
A mechanic can toss every tool into one big heap and dig around for a wrench. A smarter mechanic keeps labeled drawers: one for wrenches, one for screwdrivers, one for sockets. Each drawer holds related tools. A properties class works like a labeled drawer. All settings for one feature sit together under one prefix.
application.properties Java object app.mail.host=smtp.example.com --+ app.mail.port=587 +--> MailProperties app.mail.sender=no-reply@shop.com--+ host, port, sender
The Limits of @Value
@Value works well for one or two values. A larger group creates clutter.
@Value("${app.mail.host}")
private String host;
@Value("${app.mail.port}")
private int port;
@Value("${app.mail.sender}")
private String sender;Every key repeats the prefix, and spelling mistakes appear only at startup. A properties class removes the repetition.
Creating a Properties Class
Java records make the shortest version. Spring Boot 3 binds record components through the constructor with no extra annotation.
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(String host, int port, String sender) {
}Activate the class with a scan annotation on the main class, or list it explicitly.
@SpringBootApplication
@ConfigurationPropertiesScan
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}Setting the Values
app.mail.host=smtp.example.com
app.mail.port=587
app.mail.sender=no-reply@shop.example.comYAML expresses the same data in a tree.
app:
mail:
host: smtp.example.com
port: 587
sender: no-reply@shop.example.comUsing the Object
Inject the class like any other bean.
@Service
public class MailService {
private final MailProperties props;
public MailService(MailProperties props) {
this.props = props;
}
public String describe() {
return props.host() + ":" + props.port();
}
}Relaxed Binding
Spring Boot matches names loosely, so one setting can use several spellings.
| Source | Key Style |
|---|---|
| Properties file | app.max-retries |
| YAML file | max-retries |
| Environment variable | APP_MAXRETRIES |
| Java field | maxRetries |
All four styles map to the same field. The kebab-case style in files and the uppercase style in environment variables are the recommended choices.
Nested Objects, Lists, and Maps
@ConfigurationProperties(prefix = "app.storage")
public record StorageProperties(
String folder,
DataSize maxFileSize,
Duration cleanupAfter,
List<String> allowedTypes,
Map<String, String> labels,
Retry retry) {
public record Retry(int attempts, Duration delay) {
}
}
app.storage.folder=uploads
app.storage.max-file-size=5MB
app.storage.cleanup-after=7d
app.storage.allowed-types=image/png,image/jpeg,application/pdf
app.storage.labels.images=Pictures
app.storage.retry.attempts=3
app.storage.retry.delay=2sSpring Boot understands readable units. DataSize accepts values such as 5MB, and Duration accepts values such as 2s, 15m, or 7d.
Setting Defaults
Class-based properties can carry default values through the @DefaultValue annotation on constructor parameters of records.
@ConfigurationProperties(prefix = "app.cache")
public record CacheProperties(
@DefaultValue("10m") Duration ttl,
@DefaultValue("500") int maxEntries) {
}Applications start with sensible values even when the file omits the keys.
Validating Settings at Startup
Add the validation starter and the @Validated annotation. A bad value then stops the application at boot, which beats a hidden failure at midnight.
@Validated
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
@NotBlank String host,
@Min(1) @Max(65535) int port,
@Email String sender) {
}
Missing host --> Application refuses to start Port 99999 --> Application refuses to start Valid values --> Application starts normally
IDE Hints
The spring-boot-configuration-processor dependency generates metadata. IDEs then offer autocomplete and descriptions for your custom keys inside application.properties.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>Comparing the Two Approaches
| Feature | @Value | @ConfigurationProperties |
|---|---|---|
| Best for | One single value | Groups of related settings |
| Type conversion | Basic | Rich (lists, maps, Duration, DataSize) |
| Validation | Manual | Built in with @Validated |
| Relaxed binding | Limited | Full |
| IDE metadata | No | Yes |
Common Mistakes
Forgetting the scan annotation leaves the properties class unregistered, and injection fails at startup. A wrong prefix binds nothing, so every field stays empty. A typo in a key name produces a null value without any warning. Records whose constructor does not match the keys cause binding errors. Checking the startup log and writing one small binding test catches each of these problems in seconds.
Secrets deserve special care. Placeholders such as ${MAIL_PASSWORD} read the value from the environment, and the properties file stays safe to share with the team.
Best Practices
- Use one properties class per feature, with a clear prefix.
- Prefer records for immutable settings.
- Validate required values at startup.
- Keep secrets in environment variables and reference them with placeholders.
- Write tests that load the class with sample values.
Typed settings make configuration safe and readable. The code documents itself, and mistakes surface the moment the application starts.
