Sub topic 11
Injecting configuration with @Value and @ConfigurationProperties
This chapter assumes you can define beans with @Component or @Bean and inject them through constructors. It also assumes you use Spring Boot 3 (Spring 6), where application.properties is read automatically.
Why externalize configuration
Hostnames, ports and timeouts change between development, test and production. If you hard-code them, every change needs a rebuild. Spring instead reads them from an external source and injects them into beans. The sources include property files, environment variables and command-line arguments. Spring merges them into the Environment, and later sources override earlier ones.
Injecting single values with @Value
@Value injects one property by placeholder. A default after the colon is used when the property is missing.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MailClient {
private final String host;
private final int timeoutMillis;
public MailClient(@Value("${mail.host}") String host,
@Value("${mail.timeout:5000}") int timeoutMillis) {
this.host = host;
this.timeoutMillis = timeoutMillis;
}
}Spring converts the text to int for you. If mail.host is missing and has no default, the context fails at startup with an unresolved placeholder error. That is the behavior you want, because a misconfigured application never starts.
@Value works well for one or two values. With many related values it scatters string keys across your code. A typo in a key is only caught at startup, and the compiler never sees it.
Binding groups with @ConfigurationProperties
@ConfigurationProperties binds all properties under a prefix to a typed object. Given this file:
mail.host=smtp.example.com
mail.port=587this record receives both values:
@ConfigurationProperties(prefix = "mail")
public record MailProperties(String host, int port) {
}
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class MailConfig {
}@EnableConfigurationProperties registers MailProperties as a bean. After that you inject it like any other bean:
@Service
public class MailService {
private final MailProperties props;
public MailService(MailProperties props) {
this.props = props;
}
public String endpoint() {
return props.host() + ":" + props.port();
}
}A record has one constructor, so Spring Boot binds through it and the values are immutable. Boot also uses relaxed binding. The environment variable MAIL_HOST overrides mail.host, so the same code runs unchanged in a container.
You can add validation. Put @Validated on the properties class and constraints such as @NotBlank on its fields. This needs the spring-boot-starter-validation dependency. Startup then fails with a clear message when a value is invalid.
Choosing between them
| Need | Use |
|---|---|
| One or two values, or a SpEL expression | `@Value` |
| A group of related values | `@ConfigurationProperties` |
| Validation, nested objects, lists | `@ConfigurationProperties` |
| IDE metadata and autocompletion | `@ConfigurationProperties` |
Common errors
- Unresolved placeholder. The key is missing and has no default. Check the spelling and the active profile.
- Value not bound. The properties class was never registered. Add
@EnableConfigurationPropertiesor@ConfigurationPropertiesScan. - Conversion failure. The text cannot become the target type, for example
port=abcfor anint. Startup fails with a binding error. - Wrong prefix.
prefix = "mail"bindsmail.*only. A key such asemail.hostis silently ignored.
Exercise
The application sets MAIL_PORT=2525 as an environment variable, and application.properties contains mail.port=587. Which value does MailProperties.port() return, and why?
Show answer
It returns 2525. Environment variables rank above application.properties in Spring Boot's property source order. Relaxed binding maps MAIL_PORT to mail.port, so the higher-priority source wins.