JSR 303 bean验证,扩展的ConstraintValidator无法使用CDI [英] The JSR 303 bean validation, The extended ConstraintValidator cannot use the CDI

查看:233
本文介绍了JSR 303 bean验证,扩展的ConstraintValidator无法使用CDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在类级别上通过bean验证学习JSF 2.0,如下所示:-

I've tried to learn the JSF 2.0 with bean validation at the class level as the following: -

实用程序

@Singleton
public class MyUtility {
    public boolean isValid(final String input) {
       return (input != null) || (!input.trim().equals(""));
    }
}

约束注释

@Retention(RetentionPolicy.RUNTIME)
@Target({
    ElementType.TYPE,
    ElementType.ANNOTATION_TYPE,
    ElementType.FIELD
    })
@Constraint(validatedBy = Validator.class)
@Documented
public @interface Validatable {
    String  message() default "Validation is failure";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

约束验证器

public class Validator extends ConstraintValidator<Validatable, MyBean> {
    //
    //----> Try to inject the utility, but it cannot as null.
    //
    @Inject
    private MyUtility myUtil;

    public void initialize(ValidatableconstraintAnnotation) {
        //nothing
    }

    public boolean isValid(final MyBean myBean, 
                           final ConstraintValidatorContext constraintContext) {

        if (myBean == null) {
            return true;
        }
        //
        //----> Null pointer exception here.
        //
        return this.myUtil.isValid(myBean.getName());
    }
}

数据bean

@Validatable
public class MyBean {
    private String name;
    //Getter and Setter here
}

JSF支持bean

The JSF backing bean

@Named
@SessionScoped
public class Page1 {
    //javax.validation.Validator
    @Inject
    private Validator validator;

    @Inject
    private MyBean myBean;

    //Submit method
    public void submit() {
        Set<ConstraintViolation<Object>> violations = 
                         this.validator.validate(this.myBean);
        if (violations.size() > 0) {
            //Handle error here.
        }
    }
}

运行后,我在return this.myUtil.isValid(myBean.getName());行的名为"Validator"的类中遇到了作为java.lang.NullPointerException的异常.我知道CDI不会注入我的实用程序实例.如果我错了,请纠正我.

After running I've faced the exception as java.lang.NullPointerException at the class named "Validator" at the line return this.myUtil.isValid(myBean.getName());. I understand that the CDI does not inject my utility instance. Please correct me If I'm wrong.

我不确定我做错了什么,还是bean验证的限制.您能帮忙进一步解释吗?

I'm not sure if I'm doing something wrong or it is a bean validation limitation. Could you please help to explain further?

推荐答案

您的权利,默认情况下,Hibernate Constraint Validator未注册为CDI-Bean(尽管不能接收依赖项).

Your right, Hibernate Constraint Validator is not registered as a CDI-Bean by default (and though cannot receive dependencies).

只需将 Seam-Validation模块放在您的类路径中,一切就可以正常运行.

Just put the Seam-Validation module on your classpath, and everything should run fine.

顺便说一句:研究源代码是CDI扩展的优雅和简单的一个很好的例子.从CDI到休眠验证不需要太多的代码行.

BTW: studying the source-code of the module is an excellent example of the elegance and simplicity of CDI extension. It's doesn't need more than a few dozens lines of code to bridge from CDI to hibernate validations...

这篇关于JSR 303 bean验证,扩展的ConstraintValidator无法使用CDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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