Spring-Boot 如何正确注入 javax.validation.Validator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23604540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:16:19  来源:igfitidea点击:

Spring-Boot How to properly inject javax.validation.Validator

javaspringvalidationspring-boot

提问by WeMakeSoftware

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

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

My main configuration class is:

我的主要配置类是:

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

According to the javadocs:

根据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:

Hibernate-validator 在类路径上。我正在尝试将其注入存储库:

@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.

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

采纳答案by geoand

You need to declare a bean of type LocalValidatorFactoryBeanlike this:

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

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

in XML or

在 XML 或

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

in Java Config.

在 Java 配置中。

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 ConstraintValidatorbecause 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.

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

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

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

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

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.

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

Thispart of the Spring documentation has all the details

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