Dependency Injection in JSR-303 Constraint Validator with Spring fails

前端 未结 3 464
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: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.

提交回复
热议问题