title: Spring MVC validator annotation + custom validation
body: <p>I'm working on spring mvc application, where I should aplly validation based on Spring MVC validator. I first step for that I added annotation for class and setup controller and it works fine. And now I need to implement custom validator for perform complex logic, but i want to use existing annotation and just add additional checking.</p>
  
  <p>My User class:</p>
  
  <pre><code>public class User
  {
      @NotEmpty
      private String name;
  
      @NotEmpty
      private String login; // should be unique
  }
  </code></pre>
  
  <p>My validator:</p>
  
  <pre><code>@Component
  public class UserValidator implements Validator
  {
  
      @Autowired
      private UserDAO userDAO;
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz)
      {
          return User.class.equals(clazz) || UsersForm.class.equals(clazz);
      }
  
      @Override
      public void validate(Object target, Errors errors)
      {
          /*
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "NotEmpty.user");
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "NotEmpty.user");
          */
          User user = (User) target;
          if (userDAO.getUserByLogin(user.getLogin()) != null) {
              errors.rejectValue("login", "NonUniq.user");
          }
      }
  }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>@Controller
  public class UserController
  {
      @Autowired
      private UserValidator validator;
  
      @InitBinder
      protected void initBinder(final WebDataBinder binder)
      {
          binder.setValidator(validator);
      }
  
      @RequestMapping(value = "/save")
      public ModelAndView save(@Valid @ModelAttribute("user") final User user,
              BindingResult result) throws Exception
      {
          if (result.hasErrors())
          {
              // handle error
          } else
          {
              //save user
          }
      }
  }
  </code></pre>
  
  <p>So, Is it possible to use custom validator and annotation together? And if yes how?</p>