ConstraintValidatorFactory

WARNING - TOPIC NOT WRITTEN - TOPIC ID: 2506

This topic has not yet been written. The content below is from the topic description.
5.5. ConstraintValidatorFactory Last but not least, there is one more configuration option to discuss, the ConstraintValidatorFactory. The default ConstraintValidatorFactory provided by Hibernate Validator requires a public no-arg constructor to instantiate ConstraintValidator instances (see Section 3.1.2, “The constraint validator”). Using a custom ConstraintValidatorFactory offers for example the possibility to use dependency injection in constraint implementations. The configuration of the custom factory is once more via the Configuration (Example 5.10, “Providing a custom ConstraintValidatorFactory”). Example 5.10. Providing a custom ConstraintValidatorFactory Configuration  configuration = Validation.byDefaultProvider().configure(); ValidatorFactory factory = configuration     .constraintValidatorFactory(new IOCConstraintValidatorFactory())     .buildValidatorFactory(); Validator validator = factory.getValidator(); The interface you have to implement is: Example 5.11. ConstraintValidatorFactory interface public interface ConstraintValidatorFactory {     /**      * @param key The class of the constraint validator to instantiate.      *      * @return A constraint validator instance of the specified class.      */       > T getInstance(Class key); } Warning Any constraint implementation relying on ConstraintValidatorFactory behaviors specific to an implementation (dependency injection, no no-arg constructor and so on) are not considered portable. Note ConstraintValidatorFactory should not cache instances as the state of each instance can be altered in the initialize method.