在自定义JSR-303验证程序中使用@Autowired组件 [英] Using @Autowired component in custom JSR-303 validator

查看:91
本文介绍了在自定义JSR-303验证程序中使用@Autowired组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的模型类实现一个自定义验证器,以自动装配我的自定义bean(通过@Component声明). 在此,我遵循了本教程实现的.

I'm trying to implement a custom validator for my model classes that autowires a custom bean of mine (declared via @Component). In this, I followed the Spring documentation on that topic. My AuthenticationFacade object is implemented according to this tutorial.

但是,在运行测试时,Validator对象中的autowired属性始终为null.为什么会这样?

When running my tests, however, the autowired attribute in the Validator object is always null. Why is that?

以下是我的代码的相关部分:

Here are the relevant parts of my code:

我的自定义bean AuthenticationFacadeImpl.java

@Component
public class AuthenticationFacadeImpl implements AuthenticationFacade {
    boolean hasAnyRole(Collection<String> roles) {
        // checks currently logged in user roles
    }
}

我的自定义约束HasAnyRoleConstraint.java

@Constraint(validatedBy = HasAnyRoleConstraintValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface HasAnyRole {
    String[] value();
    String message() default "{HasAnyRole}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

我的自定义验证器HasAnyRoleConstraintValidator.java

@Component
public class HasAnyRoleConstraintValidator implements ConstraintValidator<HasAnyRole, Object> {
    @Autowired
    AuthenticationFacade authenticationFacade;

    private String[] roles;

    @Override
    public void initialize(HasAnyRole hasAnyRole) {
        this.roles = hasAnyRole.value();
    }

    @Override
    public boolean isValid(Object target, ConstraintValidatorContext constraintValidatorContext) {
        return target == null || authenticationFacade.hasAnyRole(Arrays.asList(this.roles));
    }
}

模型类Article.java

@Entity
public class Article {
    // ...
    @HasAnyRole({"EDITOR", "ADMIN"})
    private String title;
    // ...
}

服务对象ArticleServiceImpl.java

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private AuthenticationFacade authenticationFacade;

    @Autowired
    private Validator validator;

    @Override
    @PreAuthorize("hasAnyRole('ADMIN', 'EDITOR')")
    public boolean createArticle(Article article, Errors errors) {
        articleRepository.save(article);
        return true;
    }

被馈送到createArticle方法中的Errors对象旨在来自Spring控制器,该控制器被馈入带有@Valid批注的模型对象.

The Errors object that gets fed into the createArticle method is intended to come from the Spring controller, which gets fed a model object with the @Valid annotation.

存储库ArticleRepository.java使用Spring Data JPA的JpaRepository

The repository, ArticleRepository.java, uses Spring Data JPA's JpaRepository

public interface ArticleRepository extends JpaRepository<Article, Long> {
}

推荐答案

如果您的验证器是在Spring上下文之外实例化的,则可以使用Spring的AOP

If your validator is instantiated outside the Spring context, then you can use Spring’s AOP @Configurable magic to register it in context and get autowiring work. All what you need is to annotate HasAnyRoleConstraintValidator with @Configurable and enable compile time, or load time aspects weaving.

这篇关于在自定义JSR-303验证程序中使用@Autowired组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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