Sub topic 7
Binding groups of properties with @ConfigurationProperties
This chapter assumes you can read YAML, know what a Spring bean is, and have seen @Value for single values. Here you will bind a whole group of related properties to one typed object.
Why not just @Value?
@Value("${app.mail.host}") works for one value. With four or five related settings, the keys get repeated across classes and nothing checks them as a group. @ConfigurationProperties maps every key under a prefix onto the fields of one class. Spring converts the types and gives you an immutable object to inject.
The properties
app:
mail:
host: localhost
port: 1025
sender: [email protected]
retry-attempts: 3The binding class
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
String host,
int port,
String sender,
@DefaultValue("3") int retryAttempts) {
}Three details matter:
- The prefix selects the keys. Write it in lowercase kebab-case, such as
app.mail. - A record has one constructor, so Spring Boot uses it for constructor binding. Each component is matched to a key by name.
- Relaxed binding maps
retry-attemptsin the file to theretryAttemptscomponent.@DefaultValue("3")supplies a value when the key is missing. Without it, a missingintbecomes0, and a missingStringbecomesnull.
Registering the class
Annotating a class does not make it a bean. Register it in one of two ways:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}@ConfigurationPropertiesScan registers every @ConfigurationProperties class in the application's packages. To register specific classes only, use @EnableConfigurationProperties(MailProperties.class) on a configuration class.
Using it
Inject the record like any other bean:
import org.springframework.stereotype.Service;
@Service
public class MailService {
private final MailProperties mail;
public MailService(MailProperties mail) {
this.mail = mail;
}
public String describe() {
return mail.host() + ":" + mail.port() + " as " + mail.sender();
}
}Common errors
| Symptom | Likely cause |
|---|---|
| `No qualifying bean of type MailProperties` | No scan or `@EnableConfigurationProperties` registers the class |
| Startup fails with a binding or conversion error | A value cannot convert, such as `port: abc` for an `int` |
| A field is `null` or `0` | The key is misspelled, the prefix is wrong, or there is no `@DefaultValue` |
| Prefix rejected or ignored | The prefix is not kebab-case lowercase |
Binding also works on plain classes with setters and on nested records, so a group like app.mail.smtp can map to a nested type. Checking the values at startup is the subject of a later chapter.
Exercise
You add @ConfigurationProperties(prefix = "app.mail") to a record, but startup fails with "No qualifying bean of type MailProperties". What is missing, and what are two ways to fix it?
Show answer
Nothing registers the record as a bean, because the annotation alone does not create one. Add @ConfigurationPropertiesScan to the application class, or add @EnableConfigurationProperties(MailProperties.class) to a configuration class.