ProLesson 5.2Intermediate2 min readUpdated 31 October 2026
Type-Safe Configuration with @ConfigurationProperties
Replace fragile @Value annotations with type-safe, validated, immutable Java 21 record configuration classes.
01The fragility of @Value
In early Spring tutorials, developers injected configuration values using the @Value annotation:
java
// The fragile legacy approach
@Service
public class EmailService {
@Value("${app.mail.host}")
private String host;
@Value("${app.mail.port:25}")
private int port;
@Value("${app.mail.api-key}")
private String apiKey;
}Why @Value becomes a nightmare in enterprise systems:
- String-typed property paths: If you have a typo in
"${app.mail.api-key}", your IDE won't catch it. The error only surfaces when the bean is instantiated at runtime. - No validation: What if
portis configured as-5orhostis left blank?@Valuewill happily inject invalid data without complaint. - Scattershot configuration: If 8 different services need email settings, you repeat those
@Valueannotations in 8 different classes. - Poor IDE autocompletion: Your IDE cannot autocomplete properties in
application.yml.
Loading the rest of the lesson…