I just spent the last few hours working on my DiveLog project and ran into some issues implementing a custom Bean Validator. I was trying to inject a service bean into my validator, but kept getting a NullPointerException on the injected bean. After fiddling with it for half an hour or so, I started doing some research.

As it turns out, Hibernate Bean Validator's are not registered as a CDI managed beans by default, therefore ConstraintValidator implementations are not CDI managed Beans. This of course means that you can not inject any beans into them. Unfortunately there are currently no really great alternatives. You can try to look up the bean you want to inject via the JNDI tree and retrieve it that way, but that has its own little set of problems.

Allegedly Seam 3 Validation will solve this problem by bridging Hibernate Bean Validation to CDI and thus allowing you to inject beans, but after reading the documentation and following the instructions, I was ultimately unable to get it working. I eventually just ended up taking a different route for the validation that didn't involve a custom bean validator, but it is not nearly as clean of a solution.

There is Hope...

Version 1.1 of the Bean Validation specification includes plans to add this support natively, so that you do not need to do any hacks or use any 3rd party libraries. There is currently no release date for version 1.1 of the spec however.

References



Published

11 September 2012

Tags