gpt4 book ai didi

spring - 如何将(@Autowired)@Service注入(inject)ConstraintValidator?

转载 作者:行者123 更新时间:2023-12-02 07:03:56 27 4
gpt4 key购买 nike

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

我需要将该 UserService 注入(inject)到 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 的堆栈跟踪(在实现 ConstraintValidator 的类内部):

@Autowired
private UserService userService; //here

并在调用服务时

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

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

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

但这也没有帮助..我不知道我的代码有什么问题。最后是堆栈跟踪,其中包含许多异常,包括 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,并且非常感谢您的帮助,谢谢!

最佳答案

我的环境是 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);
}
}

关于spring - 如何将(@Autowired)@Service注入(inject)ConstraintValidator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com