title: Spring JPA Custom Validator Bean Inyection
body: <p>Any bean inyected into CustomValidator Implementation are always null. Im using Spring Boot 2, the applciation is a REST API, i'm not using MVC.</p>
  
  <p>I have tried everything I have read about this with no luck so far.</p>
  
  <p><a href="https://stackoverflow.com/questions/25610061/test-custom-validator-with-autowired-spring-service">this topic here for example did not work for me</a></p>
  
  <p>Should I validate otherwise?, I've been stuck with this for 2 days already.</p>
  
  <p>This is my Config class:</p>
  
  <pre>
  <code>
  
      @Configuration
      public class Beans {
  
          @Bean
          public BCryptPasswordEncoder passwordEncoder() {
  
              return new BCryptPasswordEncoder();
          }
  
          @Bean
          public MessageSource messageSource() {
  
              ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
              messageSource.setBasename("classpath:idiomas/idioma");
              messageSource.setUseCodeAsDefaultMessage(true);
              messageSource.setDefaultEncoding("UTF-8");
  
              return messageSource;
          }
  
          @Bean
          public LocaleResolver localeResolver() {
  
              AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
              resolver.setDefaultLocale(new Locale("es"));
  
              return resolver;
          }
  
          @Bean
          public LocalValidatorFactoryBean getValidator() {
  
              LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
              validatorFactoryBean.setValidationMessageSource(messageSource());
  
              return validatorFactoryBean;
          }
  
          @Bean
          public CommonsRequestLoggingFilter logFilter() {
  
              CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
              filter.setIncludeQueryString(true);
              filter.setIncludePayload(true);
              filter.setMaxPayloadLength(10000);
              filter.setIncludeHeaders(true);
              filter.setAfterMessagePrefix("REQUEST: ");
              return filter;
          }
      }
  
  </code>
  </pre>
  
  <p>My CustomValidator looks like this:</p>
  
  <pre>
  <code>
  public class AlmacenValidator implements ConstraintValidator {
  
      @Autowired
      private AlmacenService servicio;
  
      @Autowired
      private ApplicationContext contexto;
  
      @Override
      public void initialize(AlmacenValido constraintAnnotation) {
  
      }
  
      @Override
      public boolean isValid(Almacen item, ConstraintValidatorContext context) {
  
          //Database calls
      }
  </code>
  </pre>
  
  <p>JPA Entity:</p>
  
  <pre>
  <code>
  
      @Entity
      @AlmacenValido
      @Table(name = "almacenes")
      public class Almacen {
  
          //Entity fields
      }
  
  </code>
  </pre>
  
  <p>The Annotation:</p>
  
  <pre>
  <code>
  
      @Documented
      @Retention(RUNTIME)
      @Target({ TYPE, FIELD, ANNOTATION_TYPE, PARAMETER })
      @Constraint(validatedBy = AlmacenValidator.class)
      public @interface AlmacenValido {
  
          String message() default "{validacion.almacen}";
  
          Class[] groups() default {};
  
          Class[] payload() default {};
      }
  
  </code>
  </pre>