title: Using JSR-303 with custom validation
body: <p>Dear Spring community,</p>
  
  <p>What I am trying to implement is the following:</p>
  
  <ul>
  <li>I would like to have a custom validator per controller (<a href="http://forum.springsource.org/showthread.php?103761-Spring-validation" rel="nofollow">via <code>@InitBinder</code></a>)</li>
  <li>I would like Spring to call <code>validator.validate()</code> (so <a href="http://forum.springsource.org/showthread.php?95765-Valid-Not-working#post320983" rel="nofollow">not this way</a>)</li>
  <li>I would like to use JSR-303 <code>@Valid</code> annotation for that</li>
  <li>The bean to be validated (<code>RegistrationForm</code>) does not have any per-field JSR-303 annotations</li>
  <li>I don't want to include validation implementation (like <a href="http://mirrors.ibiblio.org/pub/mirrors/maven2/org/hibernate/hibernate-validator/" rel="nofollow">Hibernate</a>) into classpath; it will be useless as from above statement</li>
  </ul>
  
  <p>I basically follow the steps mentioned <a href="http://forum.springsource.org/showthread.php?88755-Commons-Validator-to-Spring-MVC-3-Validation#post314296" rel="nofollow">here</a>:</p>
  
  <ul>
  <li>I add <code>javax.validation.validation-api:validation-api</code> as my dependency</li>
  <li>I use <code>&lt;mvc:annotation-driven /&gt;</code></li>
  <li>I mark my model with <code>@Valid</code>: 
  <code>public String onRegistrationFormSubmitted(@ModelAttribute("registrationForm") @Valid RegistrationForm registrationForm, BindingResult result) ...</code></li>
  </ul>
  
  <p>So what happens, is that validation API tries to locate any implementation and fails:</p>
  
  <pre><code>Caused by: javax.validation.ValidationException: Unable to find a default provider
      at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
      at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:183)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
  </code></pre>
  
  <p>The way out is to define a <code>validator</code> property for <code>AnnotationDrivenBeanDefinitionParser</code>:</p>
  
  <pre><code>&lt;bean name="validator" class="org.company.module.RegistrationFormValidator" /&gt;
  
  &lt;mvc:annotation-driven validator="validator" /&gt;
  </code></pre>
  
  <p>but this approach means that the validator will be set to all controllers by <code>ConfigurableWebBindingInitializer.initBinder()</code>.</p>
  
  <p>I understand that I am trying to use the framework in a special way, but what the community will say, if there is a special meaning for <code>validator</code> property which tells that validator does not need to be resolved, e.g.</p>
  
  <pre><code>&lt;mvc:annotation-driven validator="manual" /&gt;
  </code></pre>
  
  <p>with special treatment:</p>
  
  <pre><code>--- AnnotationDrivenBeanDefinitionParser.java.orig      2011-06-30 14:33:10.287577300 +0200
  +++ AnnotationDrivenBeanDefinitionParser.java   2011-06-30 14:34:27.897449000 +0200
  @@ -152,6 +152,10 @@
  
          private RuntimeBeanReference getValidator(Element element, Object source, ParserContext parserContext) {
                  if (element.hasAttribute("validator")) {
  +                       if ("manual".equals(element.getAttribute("validator"))) {
  +                               return null;
  +                       }
  +
                          return new RuntimeBeanReference(element.getAttribute("validator"));
                  }
                  else if (jsr303Present) {
  </code></pre>
  
  <p>Any feedback is welcomed.</p>
  
  <p>P.S. Repost from <a href="http://forum.springsource.org/showthread.php?111456-Partially-using-JSR-303-for-validation" rel="nofollow">Spring Forum</a>.</p>