54

I've got a problem injecting the Validator into the spring application bean when attempting to validate a model using JSR-303 (hibernate-validator)

My main configuration class is:

@EnableAutoConfiguration
@EnableWebMvc // <---
@EnableJpaRepositories("com.example")
@EntityScan("com.example")
public class MainConfiguration {

According to the javadocs:

/**
 * Provide a custom {@link Validator} instead of the one created by default.
 * The default implementation, assuming JSR-303 is on the classpath, is:
 * {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
 * Leave the return value as {@code null} to keep the default.
 */
Validator getValidator();

Hibernate-validator is on the classpath. I'm trying to inject it into the Repository:

@Repository
public class UserRepositoryImpl implements UserRepositoryCustom    {

    @Autowired
    private Validator validator;

Exception being thrown:

 No qualifying bean of type [javax.validation.Validator] found for dependency:

UPDATE:

The partial work-around for this is to define this in the main configuration class:

  @Bean
    public Validator validator() {

        return new org.springframework.validation.beanvalidation.LocalValidatorFactoryBean();
    }

But integration tests (the ones which require org.springframework.test.context.web.WebAppConfiguration; annotation and use validation logic) fail.

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
  • 1
    Have you tried to configure a bean of type `LocalValidatorFactoryBean` like as per http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#validation-beanvalidation? – geoand May 12 '14 at 08:52
  • @geoand yup, tried that. If I just launch the application context without the WebMvc part, the tests are green. When I try to inject the validator into the spring controller, the tests fail (can't inject the validator). – WeMakeSoftware May 12 '14 at 09:11
  • @geoand seems that your solution works. The problem was related to the Intellij Idea internal caching / configuration. Could you post that as an answer, so that I can accept it – WeMakeSoftware May 12 '14 at 15:38
  • Glad to hear it worked! I posted it as an answer :) – geoand May 12 '14 at 17:43

2 Answers2

84

You need to declare a bean of type LocalValidatorFactoryBean like this:

<bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

in XML or

@Bean
public javax.validation.Validator localValidatorFactoryBean() {
   return new LocalValidatorFactoryBean();
}

in Java Config.

Edit:

It is important to understand that if JPA is being used and is backed by Hibernate, then Hibernate will try to automatically validate your Beans as well as the Spring Framework. This can lead to the problem of javax.validation.ValidationException: HV000064: Unable to instantiate ConstraintValidator because Hibernate doesn't know about the Spring Context and as far as I can tell there is no way to tell it, not even with the LocalValidatorFactoryBean. This causes the Validator's to run twice. One correctly, and once that fails.

In order to disable the default Hibernate ORM validation, the following property for Spring needs to be set:

spring.jpa.properties.javax.persistence.validation.mode=none

I updated this example, because it was the one I kept finding over and over again about the Validator's not being injected, and it turns out this was the problem I faced.

This part of the Spring documentation has all the details

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
geoand
  • 60,071
  • 24
  • 172
  • 190
  • 2
    One note conserning the JavaConfig. The return value of the method should be javax.validation.Validator . I'll edit your answer – WeMakeSoftware May 13 '14 at 06:51
  • 1
    @SteveGreen Glad the answer helped you! – geoand Oct 18 '15 at 08:30
  • This does not seem to work or has changed when using Spring Boot. It expects a org.springframework.validation.Validator not a javax Validator. – Lucas Aug 11 '16 at 15:59
  • 4
    shouldn't it just work without all of this, when you use @SpringBootApplication on the main application class. This should automatically included component scan & annotation-driven config for your app, which will configure correct validator. – Amrut Nov 03 '16 at 11:45
  • @Amrut I agree. Check out [this](https://github.com/spring-projects/spring-boot/issues/6574) discussion – geoand Nov 03 '16 at 14:18
1
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {LocalValidatorFactoryBean.class, ...})
@SpringBootTest
public class TestClass{
     
    @Autowired
    private Validator validator;

    @Test
    public void testValidator(){
      ...logic...
      validator.validate(...);
    }
 }

Recently ran into this issue (9/26/2023). My goal was to create a unit test where I test out the validation annotations I have on some POJOs. My validation includes the use of custom Constraint Validation Annotations as well as javax.validation annotations. Since my goal was to actually just run some validation tests against various inputs I didn't want these validation classes mocked out. I actually needed them in the test context. By default, Spring won't run the typical application boot up process where component scanning is performed and the application context is resolved. You can include the @SpringBootTest annotation test if you want that process to be performed. But this should only be done sparingly because you are essentially starting the entire application, which for my use case was not necessary. My situation is a little special in that I have some wrapper and base classes my company likes us to use. One of them implements the Spring Validator interface and also Autowires the validator interface. This was problematic as the class would call ValidationUtils.invoke(validator, target, errors) in its own overridden validate method, but since this was the only implementation of the Validator interface present in the context, it would map what was supposed to be the default validator to itself resulting in a stack overflow exception when calling the validate method of the base wrapper class. In production, the autowired dependency to a validator resolves to the default implementation, LocalValidatorFactoryBean. So in short, this needs to be added to the context. When using the ContextConfiguration annotation and the SpringBootTest annotation, we are specifying the classes to be added to the application context. I tried this also using a static TestConfiguration block in my test class but this didn't resolve my issue correctly, which is what the other solutions show.