Video in progress
What's the difference between @ConfigurationProperties and @Value, and when do you use each?
One or two values — @Value. Lots of settings under one prefix — @ConfigurationProperties: a typed class, validation, IDE autocomplete.
📖 Answer
payment:
url: https://api.pay.uz
timeout: 5s
retries: 3
@ConfigurationProperties(prefix = "payment")
@Validated
public record PaymentProperties(
@NotNull URI url,
Duration timeout,
@Min(0) @Max(10) int retries) {}
@SpringBootApplication
@ConfigurationPropertiesScan
public class App { ... }
Now you just inject PaymentProperties props into the service.
Differences:
@Value |
@ConfigurationProperties |
|
|---|---|---|
| Multiple values | each one separately | one class |
| Validation | no | yes, with @Validated |
| Relaxed binding | partial | full |
SpEL (#{...}) |
yes | no |
| IDE autocomplete | no | yes, via metadata |
A bad setting (say, retries: 50) stops the app right at startup. In production that's better than running half-broken.
Sign up to mark studied questions and save your progress.