Bean-Validation 1.1 in Glassfish 4.0 - CDI Injection not working as intended

百般思念 提交于 2019-12-03 06:43:22

CDI integration won't work out of the box when manually bootstrapping a validator via Validation.buildDefaultValidatorFactory().

When performing validation as part of the entity lifecycle via JPA, CDI integration should work; If it doesn't, there still might be an issue how Bean Validation is integrated into GlassFish. If this actually is the case, you might work around the issue by configuring a custom constraint validator factory in META-INF/validation.xml which creates CDI managed validator instances.

For this, you may use the factory provided by Hibernate Validator's CDI integration as starting point. Note that the factory needs a default constructor though when configured via XML; to satisfy that requirement you could get a reference to the BeanManager via JNDI.

I resolved. I created a Qualifier annotation:

@Qualifier   
@Retention(RUNTIME)   
@Target({TYPE,FIELD})   
public @interface Validation {

}

I annotated my ConstraintValidator:

@Validation 
public class MyValidatorImpl implements ConstraintValidator<MyValidator, MyBean> {

}

I created my ConstraintValidatorFactory:

public class CDIConstraintValidatorFactory implements ConstraintValidatorFactory {

    @Override
    public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> type) {
        try{

            T t = getBeanInstance(type);
            if(t==null){
                t = type.newInstance();
            }
            return t;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    private BeanManager getBeanManager() throws NamingException {
            String name = "java:comp/" + BeanManager.class.getSimpleName();
            InitialContext ic = new InitialContext();
            BeanManager beanManager = (BeanManager) ic.lookup(name);
            return beanManager;
    }

    public <T> T getBeanInstance(final Class<T> type) throws Exception{
        BeanManager beanManager =  getBeanManager();
        Validation v = type.getAnnotation(Validation.class);
        if(v!=null){
            final Set<Bean<?>> beans = beanManager.getBeans(type,v);
            beanManager.resolve(beans);
            if(!beans.isEmpty()){
                final Bean<T> bean = (Bean<T>) beanManager.resolve(beans);
                final CreationalContext<T> creationalContext = beanManager.createCreationalContext(bean);
                return (T) beanManager.getReference(bean, type,creationalContext);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }



}

and then i registried it my validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<validation-config    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.1.xsd">
        <constraint-validator-factory>xxx.be.framework.validator.CDIConstraintValidatorFactory</constraint-validator-factory>
</validation-config> 

regards, Giancarlo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!