Dependency Injection in JSR-303 Constraint Validator with Spring fails

前端 未结 3 459
Happy的楠姐
Happy的楠姐 2020-12-03 06:22

I have the same problem as here and here but couldn\'t find a solution yet.

So my sample test project will show the whole relevant configuration and code:

Co

相关标签:
3条回答
  • 2020-12-03 06:36

    I have fought the same problem in Spring Boot environment and I found out that Hibernate internal implementation got in instead of the configured Spring's one. When the application started, debugger caught a line with the Spring's factory but later in runtime there was Hibernate's one. After some debugging, I came to the conclusion that MethodValidationPostProcessor got the internal one. Therefore I configured it as follows:

    @Bean
    public Validator validator() {
        return new LocalValidatorFactoryBean();
    }
    
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor(Validator validator) {
        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setValidator(validator);
        return methodValidationPostProcessor;
    }
    

    Note the setter for validator - it did the job.

    0 讨论(0)
  • 2020-12-03 06:39

    This is what worked for me. I had to used the @Inject tag.

        public class FooValidator implements ConstraintValidator<FooValid, Integer> {
            private ValidationService validationService;
            @Inject
            public FooValidator(ValidationService validationService){
                this.validationService = validationService;
            }
            @Override
            public void initialize(final FooValid constraintAnnotation) {
                // TODO Auto-generated method stub
            }
            @Override
            public boolean isValid(final Integer value, final ConstraintValidatorContext context) {
                // this.validationService is always NULL!
                Assert.notNull(this.validationService, "the validationService must not be null");
                return false;
            }
    

    }

    0 讨论(0)
  • 2020-12-03 06:52

    I had the same issue. The problem is arises because Hibernate is finding and applying the validators instead of Spring. So you need to set validation mode to NONE when configuring Hibernate:

    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean
        localContainerEntityManagerFactoryBean(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean lcemfb =
            new LocalContainerEntityManagerFactoryBean();
        lcemfb.setDataSource(dataSource);
        lcemfb.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        lcemfb.setValidationMode(ValidationMode.NONE);
        // Continue configuration...
    

    If you have confgured a LocalValidationFactoryBean Spring will pick up any validators annotated with @Component and autowire them.

    0 讨论(0)
提交回复
热议问题