title: Exception java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
body: <p>I'm trying to integrate spring and Hibernate with mysql. I created a simple java project and a package with 3 classes, an application context.xml file, and an hbm.xml for mapping. But after executing, I get this error:</p>
  
  <p><strong>Error:</strong> </p>
  
  <pre><code>Sep 17, 2014 1:27:49 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
  Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'd' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at com.javaspringhibernate.InsertTest.main(InsertTest.java:14)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
      ... 10 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
      ... 20 more
  Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
      at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1281)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:435)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
      ... 30 more
  Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
      at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1273)
      ... 36 more
  </code></pre>
  
  <p>This is the <strong>Employee.java</strong> file</p>
  
  <pre><code>package com.javaspringhibernate;
  
  public class Employee {  
      private int id;  
      private String name;  
      private float salary;
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public float getSalary() {
          return salary;
      }
      public void setSalary(float salary) {
          this.salary = salary;
      }
      public Employee() {
          super();
          // TODO Auto-generated constructor stub
      }  
  
      //getters and setters  
  
  
      }  
  </code></pre>
  
  <p>This is the     <strong>InsertTest.java</strong></p>
  
  <pre><code>package com.javaspringhibernate;
  
  import org.springframework.beans.factory.BeanFactory;  
  import org.springframework.beans.factory.xml.XmlBeanFactory;  
  import org.springframework.core.io.ClassPathResource;  
  import org.springframework.core.io.Resource;  
  
  public class InsertTest {  
  public static void main(String[] args) {  
  
      Resource r=new ClassPathResource("applicationContext.xml");  
      BeanFactory factory=new XmlBeanFactory(r);  
  
      EmployeeDao dao=(EmployeeDao)factory.getBean("d");  
  
      Employee e=new Employee();  
      e.setId(114);  
      e.setName("megha");  
      e.setSalary(50000);  
  
      dao.saveEmployee(e);  
  
  }  
  }  
  </code></pre>
  
  <p>This is the <strong>EmployeeDao.java</strong>    </p>
  
  <pre><code>package com.javaspringhibernate;
  
  import org.springframework.orm.hibernate3.HibernateTemplate;  
  
  public class EmployeeDao {  
  HibernateTemplate template;  
  public void setTemplate(HibernateTemplate template) {  
      this.template = template;  
  }  
  
  public void saveEmployee(Employee e){  
      template.save(e);  
  }  
  
  public void updateEmployee(Employee e){  
      template.update(e);  
  }  
  
  public void deleteEmployee(Employee e){  
      template.delete(e);  
  }  
  }  
  </code></pre>
  
  <p>This is the <strong>employee.hbm.xml</strong>   </p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  
  &lt;hibernate-mapping&gt;  
  &lt;class name="com.javaspringhibernate.Employee" table="emp558"&gt;  
            &lt;id name="id"&gt;  
            &lt;generator class="assigned"&gt;&lt;/generator&gt;  
            &lt;/id&gt;  
  
            &lt;property name="name"&gt;&lt;/property&gt;  
            &lt;property name="salary"&gt;&lt;/property&gt;  
  &lt;/class&gt;  
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>This is the <strong>applicationContext.xml</strong>    </p>
  
  <pre><code>?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:p="http://www.springframework.org/schema/p"  
      xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;  
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" &gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/userdb" /&gt;
      &lt;property name="username" value="root" /&gt;
      &lt;property name="password" value="root" /&gt;
  
  &lt;/bean&gt; 
  
      &lt;bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
          &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;  
  
          &lt;property name="mappingResources"&gt;  
          &lt;list&gt;  
          &lt;value&gt;employee.hbm.xml&lt;/value&gt;  
          &lt;/list&gt;  
          &lt;/property&gt;  
  
          &lt;property name="hibernateProperties"&gt;  
              &lt;props&gt;  
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;  
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;  
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;  
  
              &lt;/props&gt;  
          &lt;/property&gt;  
      &lt;/bean&gt;  
  
      &lt;bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;  
      &lt;property name="sessionFactory" ref="mysessionFactory"&gt;&lt;/property&gt;  
      &lt;/bean&gt;  
  
      &lt;bean id="d" class="com.javaspringhibernate.EmployeeDao"&gt;  
      &lt;property name="template" ref="template"&gt;&lt;/property&gt;  
      &lt;/bean&gt;  
  
  &lt;/beans&gt;  
  </code></pre>