title: Spring - Injection of autowired dependencies failed
body: <p><strong>The following error appears in the server :</strong>         </p>
  
  <p>SEVERE: Context initialization failed</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.packt.webstore.domain.repository.ProductRepository com.mkyong.common.controller.ProductController.productRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.packt.webstore.domain.repository.ProductRepository] 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)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
          at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
          at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
          at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
          at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
          at javax.servlet.GenericServlet.init(GenericServlet.java:158)
          at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
          at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
          at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
          at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5262)
          at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5550)
          at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
          at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
          at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
          at java.util.concurrent.FutureTask.run(FutureTask.java:138)
          at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
          at java.lang.Thread.run(Thread.java:662)
      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.packt.webstore.domain.repository.ProductRepository com.mkyong.common.controller.ProductController.productRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.packt.webstore.domain.repository.ProductRepository] 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)}
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
  ... 29 more
  </code></pre>
  
  <p>Here is the code:</p>
  
  <p><strong>ProductController.java</strong> </p>
  
  <pre><code>package com.mkyong.common.controller;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import com.packt.webstore.domain.repository.ProductRepository;
  
  @Controller
  public class ProductController {
  
      @Autowired
      private ProductRepository productRepository;    
  
      @RequestMapping("/products")
      public String list(Model model)
      {
        model.addAttribute("product",productRepository.getAllProducts());
        return "products";
      }
  }
  </code></pre>
  
  <p><strong>Product.java</strong></p>
  
  <pre><code>package com.packt.webstore.domain;
  
  import java.math.BigDecimal;
  
  public class Product {
      private String productId;
      private String name;
      private BigDecimal unitPrice;
      private String description;
      private String manufacturer;
      private String category;
      private long unitInStock;
      private long unitInOrder;
      public String getProductId() {
          return productId;
      }
      public void setProductId(String productId) {
          this.productId = productId;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public BigDecimal getUnitPrice() {
          return unitPrice;
      }
      public void setUnitPrice(BigDecimal unitPrice) {
          this.unitPrice = unitPrice;
      }
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }
      public Product() {
          super();
      }
  
      public Product(String productId, String name, BigDecimal unitPrice) {
        this.productId = productId;
        this.name = name;
        this.unitPrice = unitPrice;
      }
  
      public String toString() {
        return "Product [productId=" + productId + ", name=" + name + "]";
      }
  
  }
  </code></pre>
  
  <p><strong>ProductRepository.java</strong>  </p>
  
  <pre><code>//interface
  
  package com.packt.webstore.domain.repository;
  
  import java.util.List;
  
  import com.packt.webstore.domain.Product;
  
  public interface ProductRepository {
    List&lt;Product&gt;getAllProducts();
  }
  </code></pre>
  
  <p><strong>InMemoryProductRepository.java</strong> </p>
  
  <pre><code>package com.packt.webstore.domain.repository.impl;
  
  import java.math.BigDecimal;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.stereotype.Repository;
  
  import com.packt.webstore.domain.Product;
  import com.packt.webstore.domain.repository.ProductRepository;
  @Repository
  public class InMemoryProductRepository implements ProductRepository {
    private List &lt;Product&gt; listOfProducts=new ArrayList&lt;Product&gt;();
  
    public InMemoryProductRepository(){
  
        Product iphone=new Product("P1234","iPhone5s",new BigDecimal(500));
      listOfProducts.add(iphone);
    }
    public List&lt;Product&gt; getAllProducts() {
          // TODO Auto-generated method stub
          return listOfProducts;
      }
  
  }
  </code></pre>
  
  <p><strong>mvc-disaptcher-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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
    &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="com.mkyong.common.controller" /&gt;
  
      &lt;bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>