title: Spring Autowiring in Validator for Dropwizard
body: <p>I'm using the Validator API to validate my Dropwizard resources.</p>
  
  <p>At this point I need to create a custom ConstraintValidator -- but my validator must be able to connect to a database.</p>
  
  <p>In order to handle this, I am using @Autowire inside my custom ConstraintValidator to provide my configured beans (the database).</p>
  
  <pre><code>public class CustomValidator implements ConstraintValidator&lt;CustomAnnotation, String&gt; {
  
      @Autowired private ApplicationContext applicationContext;
  
      @Override
      public void initialize(final CustomAnnotation customAnnotation) {
      }
  
      @Override
      public boolean isValid(final Optional&lt;String&gt; value, final ConstraintValidatorContext context) {
          applicationContext.getApplicationName();
          return true;
      }
  }
  </code></pre>
  
  <p>In order to get autowiring to work with the validator, I believe I have to use 'LocalValidatorFactoryBean'.</p>
  
  <p>I've configured mine as such:-</p>
  
  <pre><code>final LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
  factory.setApplicationContext(applicationContext); // preconfigured with my beans
  factory.afterPropertiesSet(); // seems to perform the setup of the ConstraintValidatorFactory with applicationContext.getAutowireCapableBeanFactory()
  </code></pre>
  
  <p>Dropwizard comes with a validator pre-configured in the Environment object.</p>
  
  <p>So, to ensure that Dropwizard uses the Spring configured validator with injection, I've set the environment with the LocalValidatorFactoryBean like so:-</p>
  
  <pre><code>environment.setValidator(factory);
  </code></pre>
  
  <p>However, rather frustratingly my @Autowired private ApplicationContext applicationContext from my CustomValidator still appears to be null.</p>
  
  <p>Does anyone know where I'm going wrong here or if there is an easier way?</p>
  
  <p><strong>Edit</strong> A bit more info on how it was done.</p>
  
  <p>Set this up in my application:-</p>
  
  <pre><code>final ValidatorFactory constraintValidatorFactory = // custom validator factory
  final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
  jerseyEnvironment.register(new ValidationConfigurationContextResolver(constraintValidatorFactory, factory));
  </code></pre>
  
  <p>Then used this class to replace the desired validator:-</p>
  
  <pre><code>public class ValidationConfigurationContextResolver implements ContextResolver&lt;ValidationConfig&gt; {
  
     private final ConstraintValidatorFactory constraintValidatorFactory;
     private final ValidatorFactory factory;
  
     public ValidationConfigurationContextResolver(final ConstraintValidatorFactory constraintValidatorFactory,final ValidatorFactory    factory) {
         this.constraintValidatorFactory = constraintValidatorFactory;
         this.factory = factory;
     }
  
     @Override
     public ValidationConfig getContext(final Class&lt;?&gt; type) {
         final ValidationConfig config = new ValidationConfig();
         config.messageInterpolator(factory.getMessageInterpolator());
         config.constraintValidatorFactory(constraintValidatorFactory); // custom constraint validator factory
         config.parameterNameProvider(factory.getParameterNameProvider());
         config.traversableResolver(factory.getTraversableResolver());
         return config;
     }
  </code></pre>
  
  <p>}</p>