title: Spring-MVC - Error creating bean with name 'productsController': Injection of autowired dependencies failed
body: <p>Im having a problem with a spring-mvc project. The @autowiring is not creating the beans required. Ive worked on this for over 4 days and followed all the search results. But nothing has worked. Can someone please take a look. Thank You</p>
  
  <p>The error stack is this:</p>
  
  <p>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.davis.ty.service.ProductsService com.davis.ty.controller.ProductsController.productsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.davis.ty.service.ProductsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)</p>
  
  <p>My servlet.xml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.davis.ty" /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" /&gt;
  
      &lt;bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="classpath:messages" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource"
          class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
          p:password="${jdbc.password}" /&gt;
  
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="configurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven /&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My controller is:</p>
  
  <pre><code>package com.davis.ty.controller;
  
  import java.util.Map;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import com.davis.ty.domain.Products;
  import com.davis.ty.service.ProductsService;
  
  @Controller 
  public class ProductsController {
  
      @Autowired 
      private ProductsService productsService;
  
       @RequestMapping(value = "/index", method = RequestMethod.GET)
       public String listProducts (Map&lt;String, Object&gt; map) {
  
           System.out.println("index");
  
              map.put("products", new Products());
              map.put("productsList", productsService.listProducts());
  
              return "index";
       }     
  
  
      @RequestMapping(value = "/add", method = RequestMethod.POST)
      public String addProducts(@ModelAttribute("products")
          Products products, BindingResult result) {
  
              productsService.addProducts(products);
  
              return "redirect:/index";
          }
  
      @RequestMapping("/delete/{Id}")
      public String deleteProducts(@PathVariable("Id")
          Integer Id) {
  
              productsService.removeProducts(Id);
  
              return "redirect:/index";
          }
  }       
  </code></pre>
  
  <p>My service program is:</p>
  
  <pre><code>package com.davis.ty.service;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.davis.ty.dao.ProductsDAO;
  import com.davis.ty.domain.Products;
  
  @Service
  public class ProductsServiceImpl {
  
      @Autowired
      private ProductsDAO productsDAO;
  
      @Transactional
      public void addProducts(Products products){
  
          productsDAO.addProduct(products);
      }
  
      @Transactional
      public List&lt;Products&gt; listProducts() {
  
          return productsDAO.listProducts();
  
      }
  
      @Transactional
      public void removeProducts(Integer id) {
  
          productsDAO.removeProducts(id);
  
      }
  }   
  </code></pre>