问题
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