如何(@Autowired)@Service注入ConstraintValidator?

时间:2015-07-10 11:51:22

标签: spring nullpointerexception transactions constraints

我有一些UserService在User实体上进行操作。 我创建了自己的注释和ConstraintValidator类作为实现

我需要将UserService注入ConstraintValidator。并且,正如Spring文档所说,在注册bean之后:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

SpringConstraingValidatorFactory默认注册,因此我可以轻松地@Autowired我的服务。 (来源:http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/validation.html#validation-beanvalidation-spring-constraints

不幸的是,它不起作用。我在字段上接受了带有NullPointerException的stacktrace(在实现ConstraintValidator的类中):

@Autowired
private UserService userService; //here

以及对服务的调用

@Override
public boolean isValid(String username, ConstraintValidatorContext constraintValidatorContext) {
    personService.doSomethingWithService(); //here
    return true;
}

这意味着,UserService未正确注入。我也尝试使用ApplicationContextAware - 在我的ConstraintValidator类中实现它,获取上下文并找到bean:

(UserService) context.getBean("userService");

但它也没有帮助..我不知道我的代码有什么问题。最后,stacktrace包含很多异常,包括NullPointer和RollbackException。

java.lang.NullPointerException
    com.reportme.model.validation.UsernameAvailableValidator.isValid(UsernameAvailableValidator.java:20)
    com.reportme.model.validation.UsernameAvailableValidator.isValid(UsernameAvailableValidator.java:9)
    org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:308)
    org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:180)
    org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:124)
    org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85)
    org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:463)
//...

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
//...

javax.persistence.RollbackException: Error while committing the transaction
    org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
    org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)

如果有必要,我会使用Spring 4,非常感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:-1)

我的环境是spring-boot,下面带有org.springframework.stereotype.Component注释的代码到我的验证类正在运行,FYR。

@Component
public class FooValidator implements ConstraintValidator<Foo, Object> {

  @Autowired
  MyService service;

  @Override
  public void initialize(Foo constraintAnnotation) {
  }

  @Override
  public boolean isValid(Object value, ConstraintValidatorContext context) {
    return service.isValid(value);
  }
}