title: Error creating bean with name. Combine bean validation and Spring-based validation
body: <p>I want combine bean validation and Spring validation (based on the book "Spring MVC Beginner's Guide"), but there is errors:
  My logs:</p>
  
  <pre><code>2017-03-04 19:09:54 ERROR DispatcherServlet:502 - Context initialization failed
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productValidator'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productValidator': Unsatisfied dependency expressed through field 'beanValidator'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.validation.Validator' available: expected single matching bean but found 2: org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0,validator
      at [...]
  
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productValidator': Unsatisfied dependency expressed through field 'beanValidator'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.validation.Validator' available: expected single matching bean but found 2: org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0,validator
      at [...]
  
  Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.validation.Validator' available: expected single matching bean but found 2: org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0,validator
      at [...]
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>@Controller()
  @RequestMapping("/products")
  public class ProductController {
  
      @Autowired
      private ProductService productService;
  
      @Autowired
      private ProductValidator productValidator;
  [...]
      @RequestMapping(value = "/add", method = RequestMethod.POST)
      public String processAddNewProductForm(@Valid @ModelAttribute("newProduct") Product productToBeAdded, BindingResult result,
                                             HttpServletRequest httpServletRequest){
          if(result.hasErrors())
              return "addProduct";
  
          String[] suppressedFields = result.getSuppressedFields();
          if(suppressedFields.length &gt; 0){
              throw new RuntimeException("abc" + StringUtils.arrayToCommaDelimitedString(suppressedFields));
          }
  
          MultipartFile productImage = productToBeAdded.getProductImage();
  
          String rootDirectory = httpServletRequest.getSession().getServletContext().getRealPath("/");
          if(productImage != null &amp;&amp; !productImage.isEmpty()){
              try {
                  productImage.transferTo(new File(rootDirectory + "resources\\images\\" +
                                                      productToBeAdded.getProductId() + ".png"));
              } catch (IOException e) {
                  throw new RuntimeException("abcd", e);
              }
          }
  
          productService.addProduct(productToBeAdded);
          return "redirect:/products";
      }
  
      @InitBinder
      public void initialiseBinder(WebDataBinder binder){
          binder.setValidator(productValidator);
      }
  [...]
  }
  </code></pre>
  
  <p>My class validation</p>
  
  <pre><code>@Component
  public class ProductValidator implements Validator {
      @Autowired
      private javax.validation.Validator beanValidator;
  
      private Set&lt;Validator&gt; springValidators;
  
      public ProductValidator() {
          this.springValidators = new HashSet&lt;&gt;();
      }
  
      public void setSpringValidators(Set&lt;Validator&gt; springValidators) {
          this.springValidators = springValidators;
      }
  
      @Override
      public boolean supports(Class&lt;?&gt; aClass) {
          return Product.class.isAssignableFrom(aClass);
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          Set&lt;ConstraintViolation&lt;Object&gt;&gt; constraintViolations = beanValidator.validate(target);
          for(ConstraintViolation&lt;Object&gt; constraintViolation: constraintViolations){
              String propertyPath = constraintViolation.getPropertyPath().toString();
              String message = constraintViolation.getMessage();
              errors.rejectValue(propertyPath, "", message);
          }
          for(Validator validator: springValidators){
              validator.validate(target, errors);
          }
      }
  }
  </code></pre>
  
  <p>My DispatcherServlet-context</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  [...]
  
      &lt;mvc:annotation-driven enable-matrix-variables="true"/&gt;
      &lt;mvc:resources mapping="/resource/**" location="/resources/" /&gt;
      &lt;mvc:annotation-driven enable-matrix-variables="true" validator="validator"/&gt;
  
      &lt;context:component-scan base-package="com.packt.webstore" /&gt;
  
  [...]
  
      &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"&gt;
          &lt;property name="defaultLocale" value="pl" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"&gt;
          &lt;property name="validationMessageSource" ref="messageSource"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="productValidator" class="com.packt.webstore.validator.ProductValidator"&gt;
          &lt;property name = "springValidators"&gt;
              &lt;set&gt;
                  &lt;ref bean = "unitsInStockValidator"/&gt;
              &lt;/set&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="unitsInStockValidator" class="com.packt.webstore.validator.UnitsInStockValidator"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Initially, there wasn't annotation @Component, I added it, but nothing change</p>