将Spring依赖项注入ConstrantValidator [英] Injecting Spring Dependencies into ConstrantValidator

查看:129
本文介绍了将Spring依赖项注入ConstrantValidator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bean验证.我有一个自定义验证器 @MyValidator ,该验证器需要使用注入的Spring托管DAO对象查找值.如何获得此权限?Spring不会将DAO注入我的"MyValidator"对象中.

I'm using Bean Validation. I have a custom validator @MyValidator that needs to look up a value with an injected Spring managed DAO object. How can I get access to this? Spring isn't injecting the DAO into my "MyValidator" object.

@Component
public class CodeListValidator implements ConstraintValidator<CodeList, String> {
    @Autowired
    private ICodeListCommonService codeListCommonService;

    private CodeListEnum codeListID;

    @Override
    public void initialize(CodeList constraintAnnotation) {
        this.codeListID = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return codeListCommonService.doesCodeEntityExistForCodeList(codeListID.getDbCodeListId(), value, ProviderConstants.CODE_LIST_STATUS_ACTIVE);
    }
}

"codeListCommonService"为空.这是因为Spring不会创建类-但是我如何才能将其与Spring AoP一起使用?

The "codeListCommonService" is null. This is because Spring isn't creating the class - but how can I get this to work with Spring AoP?

此验证器的用法如下:


        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        MyObject validateMe = new MyObject();

        Set<ConstraintViolation<MyObject>> constraintViolations = validator.validate(validateMe);

对于MyObject:

For MyObject:

public class MyObject {
   @Size(max = 1)
   @CodeList(CodeListEnum.CARTYPE)
   public String carType;
}

因此,当验证器运行时,它会处理注释...我只需要将服务注入到CodeListValidator中即可,可以对它进行数据库查找,以对照有效汽车类型值"的数据库列表来验证该值.

So when the validator runs, it processes the annotations... I just need to get a service injected into the CodeListValidator I made to it can do a DB lookup to verify the value against the DB list of "valid car type values".

解决方案:

尝试使Spring意识的工厂与现有代码进行过多的集成.

Played around with the idea of making a Spring aware factory- too much integration with existing code.

似乎最好的解决方案(在这里有效)是使Spring服务将ApplicationContext存储在静态方法中,以便非Spring管理的" bean可以访问它们.

The solution that seems the best (and it works here) is to make a Spring service that stores the ApplicationContext in a static method so "non-Spring managed" beans can get to them.

因此是一项新服务:

@Service
public class SpringApplicationContextService implements ISpringApplicationContextService, ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}

然后任何验证器或非Spring Bean均可通过以下方式获取Spring Bean:

And then any validator or non-Spring bean can get at the Spring beans via:

public class CodeListValidator implements ConstraintValidator<CodeList, String> {
    @Autowired
    private ICodeListCommonService codeListCommonService;

    private CodeListEnum codeListID;

    @Override
    public void initialize(CodeList constraintAnnotation) {
        this.codeListID = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        ICodeListCommonService codeListCommonService = SpringApplicationContextService.getApplicationContext().getBean(ICodeListCommonService.class);
        return codeListCommonService.doesCodeEntityExistForCodeList(codeListID.getDbCodeListId(), value, ProviderConstants.CODE_LIST_STATUS_ACTIVE);
    }
}

当然,这是所有原因的原因(在此应用中使用了数十次):

And, of course, the reason for all of this (which is used dozens of times in this app):

    @CodeList(CodeListEnum.EMAIL_OPT_OUT_FLAG)
    public String emailOptOutFlag;

    @CodeList(CodeListEnum.CLEARING_HOUSE)
    public String clearingHouse;

推荐答案

ConstraintValidator 实现中正常运行 @Autowired 的最小设置是使该bean处于Spring @Configuration :

The minimum setup for @Autowired to work properly in ConstraintValidator implementation is to have this bean in a Spring @Configuration:

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

这允许将包括 ApplicationContext 在内的所有bean直接注入到 ConstraintValidator 中:

This allows any beans, including ApplicationContext, to be injected directly into a ConstraintValidator:

@Constraint(validatedBy = DemoValidator.class)
public @interface DemoAnnotation {
    // ...
    Class<?> beanClass();
}

public class DemoValidator implements ConstraintValidator<DemoAnnotation, String> {

    private final ApplicationContext applicationContext;

    private Object bean;

    @Autowired
    public DemoValidator(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void initialize(DemoAnnotation constraint) {
        Class<?> beanClass = constraint.beanClass();
        bean = applicationContext.getBean(beanClass);
    }

    @Override
    public boolean isValid(String obj, ConstraintValidatorContext context) {
        return !obj.isEmpty();
    }
}

演示

对于真正灵活的验证解决方案,我建议使用 Jakub Jirutka的 使用Bean验证程序春季表达语言(SpEL),它允许执行以下操作:

For a really flexible validation solution I would recommend Jakub Jirutka's Bean Validator utilizing Spring Expression Language (SpEL) which allows things like:

public class Sample {

    @SpELAssert("@myService.calculate(#this) > 42")
    private int value;
}

这篇关于将Spring依赖项注入ConstrantValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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