Spring-Boot 如何正确注入 javax.validation.Validator [英] Spring-Boot How to properly inject javax.validation.Validator

查看:78
本文介绍了Spring-Boot 如何正确注入 javax.validation.Validator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用 JSR-303 (hibernate-validator) 验证模型时,我在将 Validator 注入 spring 应用程序 bean 时遇到问题

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

我的主要配置类是:

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

根据javadocs:

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 在类路径上.我正在尝试将其注入存储库:

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;

抛出异常:

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

更新:

对此的部分解决方法是在主配置类中定义它:

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();
    }

但是集成测试(需要 org.springframework.test.context.web.WebAppConfiguration; 注释和使用验证逻辑的那些)失败了.

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

推荐答案

你需要像这样声明一个 LocalValidatorFactoryBean 类型的 bean:

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

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

在 XML 或

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

在 Java 配置中.

in Java Config.

重要的是要了解,如果 JPA 正在使用并且由 Hibernate 支持,那么 Hibernate 将尝试自动验证您的 Bean 以及 Spring 框架.这可能导致 javax.validation.ValidationException: HV000064: Unable to instanceiate ConstraintValidator 的问题,因为 Hibernate 不知道 Spring Context,据我所知没有办法告诉它,甚至没有使用 LocalValidatorFactoryBean.这会导致验证器运行两次.一个正确,一旦失败.

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.

为了禁用默认的 Hibernate ORM 验证,需要为 Spring 设置以下属性:

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=无

我更新了这个例子,因为这是我一遍又一遍地发现验证器没有被注入的例子,结果证明这就是我面临的问题.

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.

这个 Spring 文档的一部分包含所有详细信息

This part of the Spring documentation has all the details

这篇关于Spring-Boot 如何正确注入 javax.validation.Validator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆