gpt4 book ai didi

spring - 使用 Spring 的 JSR-303 约束验证器中的依赖注入(inject)失败

转载 作者:行者123 更新时间:2023-12-04 03:51:18 24 4
gpt4 key购买 nike

我和 here 有同样的问题和 here但还没有找到解决方案。

所以我的示例测试项目将显示整个相关配置和代码:

约束注解:

@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FooValidator.class)
public @interface FooValid {

String message();

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}

注释 PoJo:
public class Foo {

@FooValid(message = "Test failed")
private Integer test;
[...]
}

带有@Validated 的注解服务:
@Service
@Validated
public class FooService {

private final Test test;

@Autowired
public FooService(final Test test) {
this.test = test;
}

public void foo(@Valid final Foo foo) {
this.test.test(foo);
}
}

JSR-303 约束验证器:
public class FooValidator implements ConstraintValidator<FooValid, Integer> {

@Autowired
private 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;
}

}

注入(inject)验证服务:
@Service
public class ValidationService {

public void test(final Foo foo) {
System.out.println(foo);
}
}

Spring Boot 应用及配置:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

public static void main(final String[] args) {
final ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
final FooService service = context.getBean(FooService.class);
service.foo(new Foo());
}

@Bean
public static LocalValidatorFactoryBean validatorFactory() {
return new LocalValidatorFactoryBean();
}

@Bean
public static MethodValidationPostProcessor validationPostProcessor() {
return new MethodValidationPostProcessor();
}

}

相关的maven pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.Application</start-class>
<java.version>1.7</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

我正在使用 LocalValidatorFactoryBean 默认 SpringConstraintValidatorFactory .
但是为什么依赖注入(inject)在 ConstraintValidator 中不起作用并且 ValidationService 无法 Autowiring ?

顺便说一句,如果我不在服务中使用@Validated,则在 spring 或 javax Validator 接口(interface)的对面注入(inject)并手动调用“validator.validate”,依赖注入(inject)将起作用。但我不想在每个服务中手动调用 validate 方法。

非常感谢您的帮助:)

最佳答案

我在 Spring Boot 环境中遇到了同样的问题,我发现 Hibernate 内部实现代替了配置的 Spring 实现。当应用程序启动时,调试器发现了 Spring 的工厂,但后来在运行时出现了 Hibernate 的工厂。经过一番调试,得出的结论是方法验证后处理器 得到了内部的。因此,我将其配置如下:

@Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}

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

请注意验证器的 setter - 它完成了这项工作。

关于spring - 使用 Spring 的 JSR-303 约束验证器中的依赖注入(inject)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27526620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com