Dependency Injection in JSR-303 ConstraintValidator

我只是一个虾纸丫 提交于 2019-12-08 13:27:00

问题


I have the same problem that the guy from this thread has. More precisely I get the following error, when trying to inject a bean in my custom validator that implements CustomValidator interface (it's a NPE when accessing the bean i wanted to inject):

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.
at org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:294)
at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:164)
at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:125)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:86)

...

Caused by: java.lang.NullPointerException

Do you have a solution for this? Maybe an example? Because I tried the solutions offered on the other thread and nothing worked.

Any help is appreciated. Thanks.


回答1:


You should not create validator factory on your own (Validation.buildDefaultValidatorFactory()) if you want to use Spring based constraint validators.

You should let Spring autowire correct factory to your bean:

@Controller
public class MyController {

    @Autowired
    private ValidatorFactory validatorFactory;

    // ... handler methods

}


来源:https://stackoverflow.com/questions/17083625/dependency-injection-in-jsr-303-constraintvalidator

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