Java – Autowired gives a null value in the custom constraint validator
I'm new to spring, and I've found answers to some questions on so Here are the links:
Spring 3.1 Autowiring does not work inside custom constraint validator
Autowiring a service into a validator
Autowired Repository is Null in Custom Constraint Validator
I have a spring project in which I want to use hibernate validator for object validation Based on what I read online and some forums, I try to inject validators as follows:
@Bean public Validator validator() { return new LocalValidatorfactorybean().getValidator(); }
But wherever I use it
@Autowired Validator validator;
It adopts the verifier implementation of spring instead of hibernate I couldn't figure out how to accurately inject the hibernate validator and simply assemble it into other classes automatically, so I used a cheap technique, and now my java config looks like this
@Bean public Validator validator() { // ValidatorImpl is Hibernate's implementation of the Validator return new ValidatorImpl(); }
(I really appreciate it if someone can point out how I can avoid getting the right direction of Hibernate validator in this Hacky way)
But let's look at the main problems:
This is a custom validation definition
@Target( { METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER } ) @Retention(RUNTIME) @Constraint(validatedBy = EmployeeValidator.class) @Documented public @interface EmployeeValidation { String message() default "{constraints.employeeConstraints}"; public abstract Class<?>[] groups() default {}; public abstract Class<? extends Payload>[] payload() default {}; }
My custom validator
public class EmployeeValidator implements ConstraintValidator<EmployeeValidation,Object> { @Autowired private EmployeeService employeeService; @Override public void initialize(EmployeeValidation constraintAnnotation) { //do Something } @Override public boolean isValid(String type,ConstraintValidatorContext context) { return false; } }
In the custom constraint validator above, I get employeeservice null I know that spring does not instantiate any implementation of constraintvalidator at startup, but I think adding validatorimpl () will actually solve this problem But this is not the case
Now I'm stuck in a very Hacky solution, and I don't want to continue such code Someone can help me solve my situation
Attachment: These are my imports in Java config file:
import org.hibernate.validator.HibernateValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.env.Environment; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorfactorybean; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.view.InternalResourceViewResolver;
Solution
I hope the solution can help someone:
@Bean public Validator validator () { ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class ) .configure().constraintValidatorFactory(new SpringConstraintValidatorFactory(autowireCapablebeanfactory)) .buildValidatorFactory(); Validator validator = validatorFactory.getValidator(); return validator; }
Initialize the validator using the spring constraintvalidatorfactory to inject work and provide the validator implementation as hibernate Class works in the following ways:
>Your project will be verified by the library of your choice > your custom verifier will be able to use the functions of spring and perform verification through hibernate
How this works: Hibernate's constraintvalidatorfactory does not initialize any constraintvalidators unless they are called, but spring constraintvalidatorfactory implements it by providing it with autowirecapablebeanfactory
As mentioned in @ shabyaschi's comment, to inject autowirecapablebeanfactory, you can change the method signature to:
Validator validator(final AutowireCapablebeanfactory autowireCapablebeanfactory) {
Or add getters and setters to it in the configuration file, as shown below:
public AutowireCapablebeanfactory getAutowireCapablebeanfactory() { return autowireCapablebeanfactory; } public void setAutowireCapablebeanfactory(AutowireCapablebeanfactory autowireCapablebeanfactory) { this.autowireCapablebeanfactory = autowireCapablebeanfactory; }