Hibernate validator without using autwire

I am currently working on a project in Jersey and decided to use the Hibernate validator to check the parameters. All dependencies entered into Endpoint classes are correctly initialized. However, for these dependencies in ConstraintValidator classes, it always throws NPE. So I followed the guide in the Spring + hibernate manual and registered

bean id = "validator" class = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"

and used the @Autowired annotation for services in the ConstraintValidator class that you need to enter.

Are there any side effects of its use? Is there a way to avoid auto-negotiation annotation in the ConstraintValidator class and still enter values? I tried to manually register the constraintValidator class in the context as a bean, adding a property reference for the service I needed, however it throws a null pointer exception.

+1
source share
1 answer

"The Hibernate Validator - JSR 303 Reference Implementation - Reference Guide" says something about portality:

Attention

Any restriction implementation based on ConstraintValidatorFactory implementation-specific behavior (dependency injection, no no-arg constructor, etc.) is not considered portable.

So is that bad? In my opinion, this is not so. Of course, now you are connected to the DI container (Spring) and cannot easily reuse validators (for example, when not using Spring). On the other hand, with your Spring factory validators, you can take full advantage of the framework and do very hard work (read revision data for entities and compare previous states, call arbitrary services, improve or localize message checking, ... )

One thing that you must be very careful about is that the semantics of the validator are usually read-only and should not cause side effects by causing them. For example, it is not by accident that you delete data in the database due to some automatic cleaning by calling a (transactional) service or reading the data inside your validator.

+1
source

All Articles