Autowired gives a null value in the custom constraint validator

I am completely new to Spring, and I looked at several answers on SO for the given problem. Here are the links:

Spring 3.1 Autoinstall does not work inside validator validation

Validator Service Authentication

The auto-installed repository is null in the user limiter

Basically, I'm not good enough at Spring to understand what is happening in the answers. Please come with me. I am posting my code so that I would really appreciate it if you could point me in the right direction.

I have a Spring project in which I want to use the Hibernate Validator to validate an object. Based on what I read on the Internet and in several forums, I tried to introduce a validator as follows:

@Bean
  public Validator validator() {
    return new LocalValidatorFactoryBean().getValidator();
  }

,

@Autowired
Validator validator;

Hibernate Spring. , Hibernate validator Autowire , , Java Config :

@Bean
      public Validator validator() {
        // ValidatorImpl is Hibernate implementation of the Validator
        return new ValidatorImpl();
      }

( , - , Hibernate Validator )

:

@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 {};
}

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;
}
}

null employeeService. , ConstraintValidator Spring, , ValidatorImpl() . .

, . -, , .

P.S. Java:

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; 
+3
4

, -:

@Bean
    public Validator validator () {

        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
                .configure().constraintValidatorFactory(new SpringConstraintValidatorFactory(autowireCapableBeanFactory))
                .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();


        return validator;
    }

SpringConstraintValidatorFactory, validator Hibernate.class :

  • onjects
  • Spring, , Hibernate.

: Hibernate ConstraintValidtorFactory ConstraintValidators, , SpringConstraintValidatorFactory , AutowireCapableBeanFactory.

@shabyasaschi. autowireCapableBeanFactory, :

Validator validator(final AutowireCapableBeanFactory autowireCapableBeanFactory) {

getter setter :

public AutowireCapableBeanFactory getAutowireCapableBeanFactory() {
        return autowireCapableBeanFactory;
}

public void setAutowireCapableBeanFactory(AutowireCapableBeanFactory autowireCapableBeanFactory) {
        this.autowireCapableBeanFactory = autowireCapableBeanFactory;
}
+7

bean

@Bean
public Validator validator() {
    return new LocalValidatorFactoryBean().getValidator();
}

Validator ? , javax.validation.Validator, Validator Spring.

Spring SpringConstraintValidatorFactory Hibernate Validator, .

0

:

  • , Spring.

  • , Validator.

, , .

, , ServiceUtils bean :

@Autowired
private EmployeeService employeeService;

@Override
public void initialize(EmployeeValidation constraintAnnotation) {
  //Use an utility service to get Spring beans
  employeeService = ServiceUtils.getEmployeeService();
}

ServiceUtils Spring bean , .

@Component
public class ServiceUtils {
  private static ServiceUtils instance;

  @Autowired
  private EmployeeService employeeService;

  /* Post constructor */

  @PostConstruct
  public void fillInstance() {
    instance = this;
  }

  /*static methods */

  public static EmployeeService getEmployeeService) {
    return instance.employeeService;
  }
}

, Spring , . , .

0
private XXXService xxxService = SpringContextHolder.getBean(XXXService.class);
-2

Source: https://habr.com/ru/post/1686986/


All Articles