title: Inject Spring Beans into Annotation-based Bean Validator
body: <p>I wrote a custom <code>ConstraintValidator</code> for a <code>MultipartFile</code> in a Spring MVC application that looks something like this:</p>
  
  <pre><code>@Documented
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy = {MultipartFileNotEmptyValidator.class})
  @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER })
  public @interface MultipartFileNotEmpty {
    String message() default "{errors.MultipartFileNotEmpty.message}";
    Class&lt;?&gt;[] groups() default {};
    Class&lt;? extends Payload&gt;[] payload() default {};
  }
  </code></pre>
  
  <p>and here's the <code>validatedBy</code> part:</p>
  
  <pre><code>public class MultipartFileNotEmptyValidator implements ConstraintValidator&lt;MultipartFileNotEmpty, MultipartFile&gt;
  {
    @Override
    public void initialize(MultipartFileNotEmpty annotation)
    {
      // Nothing here
    }
  
    @Override
    public boolean isValid(MultipartFile file, ConstraintValidatorContext context)
    {
      return !file.isEmpty();
    }
  }
  </code></pre>
  
  <p>This one works and is incredibly simple. What I would like to do is create a second to check and make sure the <code>MultipartFile</code> is not too large <strong>according to a value stored in a database table</strong>. Can I inject the appropriate service into the new <code>ConstraintValidator</code> class somehow in order to get the information?</p>