title: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: in Spring
body: <p>[![enter image description here][1]][1]I'm new to Spring. I was following some tutorial to make a simple insert operation using Hibernate &amp; Spring. However it ended up in some errors, I'm not able to understand the exact issue in this one. My code </p>
  
  <h2>Applicationcontext.xml</h2>
  
  <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: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&gt;  
      &lt;property name="url" value="jdbc:mysql://localhost/test"&gt;&lt;/property&gt;  
      &lt;property name="username" value="root"&gt;&lt;/property&gt;  
      &lt;property name="password" value="root"&gt;&lt;/property&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="annotatedClasses"&gt;  
          &lt;list&gt;  
              &lt;value&gt;com.hib.model.MailHib&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.dao.MailDaoCls"&gt;  
  &lt;property name="template" ref="template"&gt;&lt;/property&gt;  
  &lt;/bean&gt;  
  
  
  &lt;/beans&gt;  
  </code></pre>
  
  <h2>MailDaoCls</h2>
  
  <pre><code>package com.dao;
  
  import org.springframework.orm.hibernate3.HibernateTemplate;
  
  import com.bo.model.Mail;
  import com.hib.model.MailHib;
  
  public class MailDaoCls {
      HibernateTemplate template;  
      public void setTemplate(HibernateTemplate template) {  
          this.template = template;  
      }  
      //method to save employee  
      public void saveEmployee(MailHib e){  
          template.save(e);  
      } 
  
  }
  </code></pre>
  
  <h2>My logic</h2>
  
  <pre><code>        Resource r=new ClassPathResource("applicationContext.xml");  
      BeanFactory factory=new XmlBeanFactory(r);  
  
      MailDaoCls dao=(MailDaoCls)factory.getBean("d");
  
      MailHib mail = new MailHib();
      //mail.setMailId(1);
      mail.setFrmEMail("ffff");
      mail.setToEMail("ssss");
      mail.setSubjectEMail("fffffsf");
      mail.setMessage("messagssse");
      dao.saveEmployee(mail);
  </code></pre>
  
  <p>Error it throws is </p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>UPDATE</strong>
  after adding the commons-dbcp the error message is:</p>
  
  <pre><code>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]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/support/lob/LobHandler
  at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
  at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  at com.dao.impl.MailDaoImpl.main(MailDaoImpl.java:59)
  
  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]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/support/lob/LobHandler
  at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
  at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
  </code></pre>