title: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController':
body: <p>I am a newbie in spring and hibernate and have been studying and exploring the power these technologies bring to enterprise development. However I am stuck with a program im trying to run which keeps throwing the following errors. I found similar issue on here and have done everything suggested but still get same error.</p>
  
  <pre><code> **WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'itemMasterDao': Cannot find class [org.apache.commons.dbcp2.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/inventory-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp2.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/inventory-servlet.xml]; nested exception is java.lang.ClassNotFoundException:** org.apache.commons.dbcp2.BasicDataSource
      Aug 30, 2016 12:14:29 AM org.springframework.web.servlet.DispatcherServlet initServletBean
      SEVERE: Context initialization failed
      org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'itemMasterDao': Cannot find class [org.apache.commons.dbcp2.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/inventory-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp2.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/inventory-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
          at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
          at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
          at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
          at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
          at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
          at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
          at javax.servlet.GenericServlet.init(GenericServlet.java:158)
          at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
          at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
          at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
          at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
          at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5337)
          at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
          at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
          at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
          at java.util.concurrent.FutureTask.run(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          at java.lang.Thread.run(Unknown Source)
      Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp2.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/inventory-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource
          at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1357)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
          at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1450)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:446)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:424)
          at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:220)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1193)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1048)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
  
  
  
      inventory-servlet.xml
  
  
  
      &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;beans xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://www.springframework.org/schema/beans"
          xmlns:context="http://www.springframework.org/schema/context"
          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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;context:component-scan base-package="com.chills.hspring.product.controller, com.chills.hspring.product.dao" /&gt;
  
          &lt;bean id="itemMasterDao" class="com.chills.hspring.product.dao.ItemMasterDAOImpl"&gt;
              &lt;constructor-arg&gt;
                  &lt;ref bean="sessionFactory" /&gt;
              &lt;/constructor-arg&gt;
          &lt;/bean&gt;
  
             &lt;bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"&gt;
              &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt;
              &lt;property name="username" value="root" /&gt;
              &lt;property name="password" value="j123456" /&gt;
          &lt;/bean&gt;
  
  
       &lt;bean id="sessionFactory"    Class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
          &lt;/bean&gt;
  
          &lt;tx:annotation-driven /&gt; 
          &lt;bean id="transactionManager"   class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
          &lt;/bean&gt;
  
  
  
          &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
              &lt;property name="prefix" value="/Pages/"&gt;&lt;/property&gt;
              &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt;        
          &lt;/bean&gt;
  
      &lt;/beans&gt;
  
  
      web.xml
  
      &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;web-app version="2.4"
               xmlns="http://java.sun.com/xml/ns/j2ee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
               http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" &gt;
  
        &lt;servlet&gt;
          &lt;servlet-name&gt;inventory&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;inventory&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
        &lt;/servlet-mapping&gt;
  
      &lt;/web-app&gt;
  
  
      hibernate.cfg.xml
  
  &lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
          &lt;property name="connection.driver_class"&gt;
              com.mysql.jdbc.Driver
          &lt;/property&gt;
          &lt;property name="connection.url"&gt;
              jdbc:mysql://localhost:3306/test
          &lt;/property&gt;
  
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;
              org.hibernate.dialect.MySQLDialect
          &lt;/property&gt;
  
          &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;
              org.hibernate.cache.NoCacheProvider
          &lt;/property&gt;
  
          &lt;!-- Display all generated SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;mapping class="com.chills.hspring.dto.ItemMaster" /&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  
  
  
  
      ItemMasterDAOImpl.java
  
      package com.chills.hspring.product.dao;
  
      import java.util.List;
  
      import org.hibernate.HibernateException;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.springframework.transaction.annotation.Transactional;
  
      import com.chills.hspring.dto.ItemMaster;
  
      @Transactional
      public class ItemMasterDAOImpl implements ItemMasterDAO {
  
          private SessionFactory sessionFactory;
  
          public ItemMasterDAOImpl(SessionFactory sessionFactory) {
  
              this.sessionFactory =  sessionFactory;
          }
          @Override
          public void add(ItemMaster item) {
  
              Session session = sessionFactory.getCurrentSession();
              try {
  
                  session.beginTransaction();
                  session.save(item);
              }catch(HibernateException e) {
                  e.printStackTrace();
                  session.getTransaction().rollback();
              }
              session.getTransaction().commit();
  
          }
          @Override
          public void update(ItemMaster item) {
  
              Session session = sessionFactory.getCurrentSession();
              try {
                  System.out.println("IN UPDATE");
                  session.beginTransaction();
                  session.saveOrUpdate(item);
              }catch(Exception e) {
                  e.printStackTrace();
                  session.getTransaction().rollback();
              }
              session.getTransaction().commit();
  
          }
  
          @Override
          public ItemMaster getItem(Long id) {
  
              Session session = sessionFactory.getCurrentSession();
              ItemMaster item = null;
              try {
                  System.out.println("IN GETITEM");
                  session.beginTransaction();
                  item = (ItemMaster) session.get(ItemMaster.class, id);
              }catch(HibernateException e) {
  
                  e.printStackTrace();
                  session.getTransaction().rollback();
              }
              session.getTransaction().commit();
              return item;
  
          }
          @Override
          public void delete(Long id) {
  
              Session session =  sessionFactory.getCurrentSession();
              session.beginTransaction();
              ItemMaster item = (ItemMaster)session.get(ItemMaster.class, id);
              if(null != item) {
                  session.delete(item);
              }
              session.getTransaction().commit();
          }
          @SuppressWarnings({ "deprecation", "unchecked" })
          @Override
          public List&lt;ItemMaster&gt; list() {
  
              Session session = sessionFactory.getCurrentSession();
              session.beginTransaction();
              List&lt;ItemMaster&gt; items = null;
              try {
                  System.out.println("IN LIST");
                  items = (List&lt;ItemMaster&gt;)session.createQuery("from ItemMaster").list();
              }catch(HibernateException e) {
  
                  e.printStackTrace();
                  session.getTransaction().rollback();
              }
              session.getTransaction().commit();
              return items;
  
          }
  
  
      }
  
      ItemMasterDAO.java
  
      package com.chills.hspring.product.dao;
  
      import java.util.List;
  
      import com.chills.hspring.dto.*;
  
      public interface ItemMasterDAO {
  
          public List&lt;ItemMaster&gt; list();
          public void add(ItemMaster item);
          public void update(ItemMaster item);
          public ItemMaster getItem(Long id);
          public void delete(Long id);
  
      }
  
      ItemMaster.java
  
      package com.chills.hspring.dto;
  
      import java.io.Serializable;
      import java.util.Date;
  
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
  
      @Entity
      @Table(name="ItemMaster")
      public class ItemMaster implements Serializable {
  
          private static final long serialVersionUID = 45369355259515150L;
          private Long item_code;
          private String item_name;
          private double price;
          private int qty;
  
          @Temporal(TemporalType.DATE)
          @Column (name="createdOn")
          private Date createdOn;
  
          public Date getCreatedOn()
          {
              return createdOn;
          }
          public void setCreatedOn(Date createdOn)
          {
              this.createdOn =  createdOn;
          }
          @Id
          @GeneratedValue
          @Column(name="item_code")
          public Long getItem_code()
          {
              return item_code;
          }
          public void setItem_code(Long item_code)
          {
              this.item_code =  item_code;
          }
          @Column(name="item_name")
          public String getItem_name()
          {
              return item_name;
          }
          public void setItem_name(String item_name)
          {
              this.item_name =  item_name;
          }
          @Column(name="price")
          public double getPrice()
          {
              return price;
          }
          public void setPrice(double price)
          {
              this.price = price;
          }
          @Column (name="qty")
          public int getQty()
          {
              return qty;
          }
          public void setQty(int qty)
          {
              this.qty = qty;
          }
  
  
      }
  
  
      ProductController.java
  
  
      package com.chills.hspring.product.controller;
  
      import java.util.List;
  
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.portlet.ModelAndView;
      import com.chills.hspring.product.dao.*;
  
      import com.chills.hspring.dto.ItemMaster;
  
      @Controller
      public class ProductController {
  
          @Autowired
          private ItemMasterDAO itemMasterDao;
  
  
          @RequestMapping(value="/list")
          public ModelAndView list() {
              List&lt;ItemMaster&gt; itemList = itemMasterDao.list();
              ModelAndView model = new ModelAndView("itemMasterList");
              model.addObject("itemList",itemList);
              return model;
          }
          @RequestMapping(value="/loadAddForm")
          public ModelAndView add() {
  
              ModelAndView model = new ModelAndView("ItemMasterAdd");
              ItemMaster item =  new ItemMaster();
              model.addObject("item", item);
              List&lt;ItemMaster&gt; itemList = itemMasterDao.list();
              model.addObject("itemList",itemList);
              return model;
          }
          @RequestMapping(value="/edit")
          public ModelAndView edit(@RequestParam(value="id", required = true)Long id) {
  
              System.out.println("Id = "+ id);
              ModelAndView model = new ModelAndView("ItemMasterAdd");
              ItemMaster item = itemMasterDao.getItem(id);
              model.addObject("item", item);
              List&lt;ItemMaster&gt;itemList = itemMasterDao.list();
              model.addObject("itemList",itemList);
              return model;
          }
          @RequestMapping(name="/delete")
          public ModelAndView delete(@RequestParam(value="id", required = true)Long id) {
              ModelAndView model =  new ModelAndView("ItemMasterAdd");
              itemMasterDao.delete(id);
              List&lt;ItemMaster&gt; itemList = itemMasterDao.list();
              model.addObject("itemList", itemList);
              return model;
          }
          @RequestMapping(name="/save", method = RequestMethod.POST)
          public ModelAndView save(@ModelAttribute("item")ItemMaster item) {
  
              System.out.println(item.getItem_name());
              if(null != item)
                  itemMasterDao.add(item);
              ModelAndView model =  new ModelAndView("ItemMasterAdd");
              item = new ItemMaster();
              model.addObject("item",item);
              List&lt;ItemMaster&gt; itemList = itemMasterDao.list();
              model.addObject("itemList", itemList);
              return model;
          }
          @RequestMapping(value = "/update", method = RequestMethod.POST)
          public ModelAndView update(@ModelAttribute("item")ItemMaster item) {
  
              System.out.println(item.getItem_name());
              if(null != item)
                  itemMasterDao.update(item);
              ModelAndView model = new ModelAndView("ItemMasterAdd");
              item  = new ItemMaster();
              model.addObject("item",item);
              List&lt;ItemMaster&gt; itemList = itemMasterDao.list();
              model.addObject("itemList", itemList);
              return model;
          }
  
  
  
      }
  </code></pre>