Spring-Boot How to properly inject javax.validation.Validator
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
In Spring Boot, you normally inject the Bean Validation Validator as a regular Spring bean rather than creating it yourself. The main complication is versioning: Spring Boot 2 uses javax.validation.Validator, while Spring Boot 3 uses jakarta.validation.Validator.
Let Spring Boot Create the Validator
If the validation starter is on the classpath, Spring Boot usually auto-configures a validator for you.
Maven:
That gives you a LocalValidatorFactoryBean behind the scenes and exposes it through the validation interface. In a Boot 2 application, that means you can inject javax.validation.Validator directly.
Constructor injection is the right default because the dependency is explicit and testable.
javax.validation Versus jakarta.validation
The package name matters.
If you are on:
- Spring Boot 2.x, inject
javax.validation.Validator - Spring Boot 3.x, inject
jakarta.validation.Validator
The usage pattern is almost identical, but the import is not.
That means code copied from older blog posts can fail to compile after a Boot 3 upgrade even if the surrounding logic is still correct.
Manual Validation Versus Automatic Validation
There are two common validation styles in Spring Boot.
Automatic request validation
This is ideal when validation belongs at the HTTP boundary.
Manual validation in service code
This is useful when validation needs to happen:
- outside controllers
- in batch workflows
- in event handlers
- before calling deeper business logic
Do Not Build the Validator Manually Unless Necessary
A lot of examples online show direct factory creation. In Boot, that is usually unnecessary.
Avoid doing this unless you have a specific customization need:
That bypasses Spring's configuration model and can make testing or message configuration less consistent.
If you really need custom validator setup, define a bean intentionally.
But do this only when you need specific behavior such as message-source integration or custom constraint wiring.
Method Validation Is Separate
Spring can also validate method parameters on beans when method validation is enabled.
That does not replace injected Validator, but it is powered by the same validation infrastructure.
Choose the style based on where the rule belongs, not just on which API looks shorter.
Common Pitfalls
- Injecting
javax.validation.Validatorin a Spring Boot 3 project that usesjakarta.validation.Validator. - Creating a validator manually when Boot already provides one.
- Using field injection instead of constructor injection, which makes tests and dependencies less explicit.
- Expecting
@Validto handle validation in arbitrary service methods without method validation support. - Overriding the default validator bean without a clear reason.
Summary
- In Spring Boot, the validator is usually auto-configured for you.
- Inject
Validatorthrough the constructor instead of building it manually. - Use
javax.validationon Boot 2 andjakarta.validationon Boot 3. - Prefer automatic validation at boundaries and manual validation where business flow requires it.
- Customize the validator bean only when you have a concrete need.
Related reading
- Spring Async not allowing use of autowired beans
- Spring Autowired usage
- Spring Beanname name vs Bean Qualifiername
- Spring Boot - inject map from application.yml
- Spring-Boot logging to Kafka how to eliminate warning; best practices
- Spring-Boot logging with log4j2?
- Spring boot - Service class calling another Service class
- Spring Boot 2 - Unsatisfied dependency on Feign client when autowired for service

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.