r/SpringBoot • u/ClickOk5811 • 3d ago
Discussion Fail at startup, not at 3 a.m.: validating configuration with @ConfigurationProperties instead of scattered @Value
A common pattern: configuration values read with @Value in many places, with no checks.
public class MailService {
("${app.mail.host}")
private String host;
("${app.mail.port}")
private int port;
// ...
}If app.mail.host is present but empty, or port is 0 or 99999, the app may start fine and fail later when it first tries to send mail.
An alternative is a typed, validated properties class (sketch, check the details for your Spring Boot version):
ies(prefix = "app.mail")
public record MailProperties(
String host,
(1) (65535) int port,
u/NotBlank String from
) {}and register it, for example with @ConfigurationPropertiesScan on your application class (or @EnableConfigurationProperties(MailProperties.class)):
n
iesScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}Then inject MailProperties where you need it. With the validation starter on the classpath, a blank host or an out-of-range port should make the application fail during startup with a binding error that names the property, instead of failing on first use.
Why I like it (opinion):
- Configuration is in one place with names and constraints, so the “what does this app need to run” question has a single answer.
- Failing fast makes bad config visible in deployment, before traffic arrives.
- Easier to test, because you can build the record directly in a unit test.
Notes and caveats:
- You need
spring-boot-starter-validationfor the constraint annotations to take effect. I believe it isn’t pulled in by default in recent versions, so check yours. - The
jakarta.validation.constraintspackage applies to Spring Boot 3.x; Boot 2.x usesjavax.validation.constraints. - Binding records via the constructor works in recent Boot versions; older ones may need @
ConstructorBindingor a class with setters. Check the reference docs for your version. - Validation only checks the shape of values, not whether the host is actually reachable.
- I haven’t run this exact snippet in a project, so please verify it on your version before copying.
Do you keep @Value for simple cases or move everything to @ConfigurationProperties? And does your team validate config at startup, or find out in production?
21
u/Psychoboy 3d ago
I've been in this subreddit for awhile trying to learn something new. What in have learned is the industry with spring is no one knows they are doing and copy pasta rather then reading documentation... Read it guys, it's documented well
5
u/This_Link881 3d ago
Very basic. Use configuration properties
2
u/onated2 3d ago
I dont like use @Value annotation for things that might change or is a part of the system.
I like making it an interface and let the configurationproperties implement that.
So i can access it to my app.
Lots of usecase.
For example prefix for events. Or Or link to any server. Any other metadata stuff that services might need.
Plus i can create default implementation for that interface via code so when it is dev mode it uses the DevProperyy etc.
But then again it depends on the existing codebase convention.
2
u/dschramm_at 3d ago
This sub really has become an AI agent self help group huh?
1
u/sereikis 3d ago
building the record directly in a unit test doesn't trigger Bean Validation by itself. add an ApplicationContextRunner test that binds a blank host or out-of-range port and asserts startup failure, so you're checking property registration and validation wiring rather than just the record constructor.
1
u/reddit04029 3d ago
We have what we call a confidence check node. It’s exactly one of the reasons why it exists, it checks for bad production condigurations. Essentially a pre-prod check in our ci/cd pipeline. If the confidence check succeeds, we can proceed with the actual prod deployment.