小编oil*_* ID的帖子

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

我和这里这里有同样的问题,但还没有找到解决方案.

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

约束注释:

@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 {};
}
Run Code Online (Sandbox Code Playgroud)

注释PoJo:

public class Foo {

    @FooValid(message = "Test failed")
    private Integer test;
    [...]
}
Run Code Online (Sandbox Code Playgroud)

使用@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

JSR-303 ConstraintValidator:

public …
Run Code Online (Sandbox Code Playgroud)

validation spring dependency-injection

8
推荐指数
2
解决办法
8272
查看次数

标签 统计

dependency-injection ×1

spring ×1

validation ×1