title: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
body: <p>I am new here and I am trying to create an e-shop using Spring but I have a problem with the connection to database. I searched and found that other users asked the same thing but I cannot find the problem. I lack of ideas, what is wrong here.</p>
  
  <p><strong>Stacktrace:</strong></p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'homeController': Injection of autowired
    dependencies failed; nested exception is
    org.springframework.beans.factory.BeanCreationException: Could not
    autowire field: private dao.ProductDao
    controllers.HomeController.productDao; nested exception is
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    qualifying bean of type [dao.ProductDao] 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)}</p>
  </blockquote>
  
  <p><strong>Source code:</strong></p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>package controllers;
  
  import java.io.IOException;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import dao.ProductDao;
  import model.Product;
  
  @Controller
  public class HomeController {
  
  @Autowired
  private ProductDao productDao;
  
  @RequestMapping("/")
  public String home(){
      return "home";
  }
  
  @RequestMapping("/productList")
  public String getProducts(Model model) {
      List&lt;Product&gt; products = productDao.getAllProducts();
      model.addAttribute("products", products);
  
      return "productList";
  }
  
  @RequestMapping("/productList/viewProduct/{productId}")
  public String viewProduct(@PathVariable String productId, Model model) throws IOException{
  
      Product product = productDao.getProductById(productId);
      model.addAttribute(product);
      return "viewProduct";
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>ProductDao</strong></p>
  
  <pre><code>package dao;
  import java.util.List;
  import org.springframework.stereotype.Component;
  import model.Product;
  public interface ProductDao {
      void addProduct(Product product);
      Product getProductById(String id);
      List&lt;Product&gt; getAllProducts();
      void deleteProduct(String id);
  }
  </code></pre>
  
  <p><strong>ProductdaoImpl</strong></p>
  
  <pre><code>    package dao.impl;
  
      import java.util.List;
  
      import org.hibernate.Query;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Repository;
      import org.springframework.transaction.annotation.Transactional;
  
      import dao.ProductDao;
      import model.Product;
  
      @Repository
      @Transactional
      public class ProductDaoImpl implements ProductDao {
  
          @Autowired
          private SessionFactory sessionFactory;
  
          public void addProduct(Product product) {
              Session session = sessionFactory.getCurrentSession();
              session.saveOrUpdate(product);
              session.flush();
          }
  
          public Product getProductById(String id) {
              Session session = sessionFactory.getCurrentSession();
              Product product = (Product) session.get(Product.class, id);
              session.flush();
  
              return product;
          }
  
          public List&lt;Product&gt; getAllProducts() {
              Session session = sessionFactory.getCurrentSession();
              Query query = session.createQuery("from Product");
              List&lt;Product&gt; products = query.list();
              session.flush();
  
              return products;
          }
  
          public void deleteProduct(String id) {
              Session session = sessionFactory.getCurrentSession();
              session.delete(getProductById(id));
              session.flush();
          }
      }
  </code></pre>
  
  <p><strong>Entity (Product)</strong></p>
  
  <pre><code>    package model;
  
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
  
      @Entity
      public class Product {
  
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private String productId;
          private String productName;
          private String productCategory;
          private String productDescription;
          private double productPrice;
          private String productCondition;
          private String productStatus;
          private int unitInStock;
          private String productManufacture;
  
          public String getProductId() {
              return productId;
          }
  
          public void setProductId(String productId) {
              this.productId = productId;
          }
  
          public String getProductName() {
              return productName;
          }
  
          public void setProductName(String productName) {
              this.productName = productName;
          }
  
          public String getProductCategory() {
              return productCategory;
          }
  
          public void setProductCategory(String productCategory) {
              this.productCategory = productCategory;
          }
  
          public String getProductDescription() {
              return productDescription;
          }
  
          public void setProductDescription(String productDescription) {
              this.productDescription = productDescription;
          }
  
          public double getProductPrice() {
              return productPrice;
          }
  
          public void setProductPrice(double productPrice) {
              this.productPrice = productPrice;
          }
  
          public String getProductCondition() {
              return productCondition;
          }
  
          public void setProductCondition(String productCondition) {
              this.productCondition = productCondition;
          }
  
          public String getProductStatus() {
              return productStatus;
          }
  
          public void setProductStatus(String productStatus) {
              this.productStatus = productStatus;
          }
  
          public int getUnitInStock() {
              return unitInStock;
          }
  
          public void setUnitInStock(int unitInStock) {
              this.unitInStock = unitInStock;
          }
  
          public String getProductManufacture() {
              return productManufacture;
          }
  
          public void setProductManufacture(String productManufacture) {
              this.productManufacture = productManufacture;
          }
  
      }
  </code></pre>
  
  <p><strong>jdbc.properties</strong></p>
  
  <pre><code>  jdbc.username = ****
    jdbc.password = ****
    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://localhost:3306/myeshop
  </code></pre>
  
  <p><strong>applicationContext.xml</strong></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:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="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-4.1.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"&gt;
  
          &lt;beans profile="dev"&gt;
              &lt;context:property-placeholder
                  location="properties/jdbc.properties" /&gt;
              &lt;bean id="dataSource"
                  class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
                  &lt;property name="driverClassName" value="${jdbc.driver}"&gt;&lt;/property&gt;
                  &lt;property name="url" value="${jdbc.url}"&gt;&lt;/property&gt;
                  &lt;property name="password" value="${jdbc.password}"&gt;&lt;/property&gt;
                  &lt;property name="username" value="${jdbc.username}"&gt;&lt;/property&gt;
              &lt;/bean&gt;
  
              &lt;bean id="sessionFactory"
                  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
                  &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
                  &lt;property name="hibernateProperties"&gt;
                      &lt;props&gt;
                          &lt;prop key="hibernate.diaect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
                          &lt;!-- &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt; 
                               &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                               &lt;prop key="hibernate.format_sql"&gt;tru&lt;/prop&gt;
                          --&gt;
                      &lt;/props&gt;
                  &lt;/property&gt;
                  &lt;property name="packagesToScan"&gt;
                      &lt;list&gt;
                          &lt;value&gt;dao&lt;/value&gt;
                      &lt;/list&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
  
      &lt;!-- Exei alli klasi sto e-commerce me ref sto SessionFactory  
              &lt;bean id="transactionManager"
                  class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
                  &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
              &lt;/bean&gt;
      --&gt;
              &lt;bean id="transactionManager"
                  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
                  &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
              &lt;/bean&gt;
              &lt;tx:annotation-driven /&gt;
          &lt;/beans&gt;
  
      &lt;/beans&gt;
  </code></pre>
  
  <p><strong>dispatcher-servlet.xml</strong></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:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc" 
          xmlns:tx="http://www.springframework.org/schema/tx"
          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-4.1.xsd"&gt;
  
          &lt;context:component-scan base-package="controllers" /&gt;
  
          &lt;mvc:annotation-driven /&gt;
  
          &lt;bean id="viewResolver"
              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;mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" /&gt;
  
          &lt;tx:annotation-driven /&gt;
      &lt;/beans&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
          version="3.1"&gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
              &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
              &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
          &lt;/servlet&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/&lt;/url-pattern&gt;
          &lt;/servlet-mapping&gt;
  
          &lt;!-- add listener --&gt;
  
          &lt;description&gt;eShop Database&lt;/description&gt;
          &lt;resource-ref&gt;
              &lt;description&gt;DB Connection&lt;/description&gt;
              &lt;res-ref-name&gt;jdbc/myeshop&lt;/res-ref-name&gt;
              &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
              &lt;res-auth&gt;Container&lt;/res-auth&gt;
          &lt;/resource-ref&gt;
  
          &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
          &lt;/listener&gt;
          &lt;context-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;
              WEB-INF/dispatcher-servlet.xml
              WEB-INF/applicationContext.xml
          &lt;/param-value&gt;
          &lt;/context-param&gt;
          &lt;!-- end of adding listener --&gt;
      &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>    &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  
          &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
          &lt;groupId&gt;com.mywebsite&lt;/groupId&gt;
          &lt;artifactId&gt;emusicstore&lt;/artifactId&gt;
          &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
          &lt;packaging&gt;war&lt;/packaging&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
                  &lt;version&gt;4.1.4.RELEASE&lt;/version&gt;
              &lt;/dependency&gt;
  
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
                  &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
                  &lt;version&gt;4.1.4.RELEASE&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                  &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
                  &lt;version&gt;4.0.1.Final&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
                  &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
                  &lt;version&gt;1.0.1.Final&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;mysql&lt;/groupId&gt;
                  &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
                  &lt;version&gt;6.0.5&lt;/version&gt;
              &lt;/dependency&gt;
  
  
  
              &lt;dependency&gt;
                  &lt;groupId&gt;jstl&lt;/groupId&gt;
                  &lt;artifactId&gt;jstl&lt;/artifactId&gt;
                  &lt;version&gt;1.2&lt;/version&gt;
              &lt;/dependency&gt;
  
              &lt;dependency&gt;
                  &lt;groupId&gt;taglibs&lt;/groupId&gt;
                  &lt;artifactId&gt;standard&lt;/artifactId&gt;
                  &lt;version&gt;1.1.2&lt;/version&gt;
              &lt;/dependency&gt;
  
          &lt;/dependencies&gt;
  
          &lt;build&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.5.1&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;source&gt;1.8&lt;/source&gt;
                          &lt;target&gt;1.8&lt;/target&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/build&gt;        
      &lt;/project&gt;
  </code></pre>
  
  <p>Thanks in advance! :)</p>