title: Error creating bean with name 'dataSource' defined in ServletContext resource
body: <p>I am working on struts2 demo project and spring to manage Hibernate sessions.</p>
  
  <p>I am following this article <a href="http://www.splinter.com.au/using-spring-to-manage-hibernate-sessions-in/" rel="nofollow noreferrer">http://www.splinter.com.au/using-spring-to-manage-hibernate-sessions-in/</a></p>
  
  <p><strong>ERROR</strong></p>
  
  <pre><code>  SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchFieldError: NULL
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
      at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      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:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  Caused by: java.lang.NoSuchFieldError: NULL
      at org.springframework.expression.TypedValue.&lt;clinit&gt;(TypedValue.java:32)
      at org.springframework.expression.spel.support.StandardEvaluationContext.setRootObject(StandardEvaluationContext.java:85)
      at org.springframework.expression.spel.support.StandardEvaluationContext.&lt;init&gt;(StandardEvaluationContext.java:74)
      at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:124)
      at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1299)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:210)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:182)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
      ... 21 more
  Sep 24, 2012 7:22:18 AM org.apache.catalina.core.StandardContext startInternal
  SEVERE: Error listenerStart
  Sep 24, 2012 7:22:18 AM org.apache.catalina.core.StandardContext startInternal
  SEVERE: Context [/1-Struts2_CRUD] startup failed due to previous errors
  Sep 24, 2012 7:22:18 AM org.apache.catalina.core.ApplicationContext log
  INFO: Closing Spring root WebApplicationContext
  Sep 24, 2012 7:22:18 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
  SEVERE: The web application [/1-Struts2_CRUD] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
  Sep 24, 2012 7:22:18 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
  SEVERE: The web application [/1-Struts2_CRUD] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak.
  </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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
      &lt;!-- enable annotation beans definitions --&gt;
      &lt;context:component-scan base-package="com.pirzada"/&gt;
  
      &lt;!-- Basic DataSource Configuration --&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/struts2_demo_db"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value="786"/&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="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.pirzada.dao&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.MySQL5Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.useUnicode"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.characterEncoding"&gt;UTF-8&lt;/prop&gt;
                  &lt;prop key="hibernate.max_fetch_depth"&gt;0&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.use_scrollable_resultset"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- defining transaction manager bean using Hibernate --&gt;
      &lt;bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- enable the configuration of transactional behavior based on annotations --&gt;
      &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  
      &lt;!-- &amp;lt;!&amp;ndash; exception translation bean post processor &amp;ndash;&amp;gt;
     &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
     &lt;bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/&gt;--&gt;
  
      &lt;!--  DAO beans --&gt;
      &lt;!--
      &lt;bean id="userDAO"  class="org.droidaceapps.dao.UserDAOImpl"&gt;
          &lt;property name="hibernateTemplate" ref="hibernateTemplate" /&gt;
      &lt;/bean&gt;
  
      &amp;lt;!&amp;ndash;  Service beans &amp;ndash;&amp;gt;
  
      &lt;bean id="userService"  class="org.droidaceapps.services.UserServiceImpl"&gt;
          &lt;property name="userDAO" ref="userDAO" /&gt;
      &lt;/bean&gt;--&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0"&gt;
  
    &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  
      &lt;!--&lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;--&gt;
  
  
      &lt;filter&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;filter-class&gt;
              org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
          &lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;!-- Spring --&gt;
      &lt;listener&gt; &lt;!-- Required for the struts2 spring plugin to work --&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;filter&gt; &lt;!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work --&gt;
          &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;sessionFactoryBeanName&lt;/param-name&gt;
              &lt;param-value&gt;sessionFactory&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;/index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>Hibernate.cfg.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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/struts2_demo_db&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;786&lt;/property&gt;
          &lt;property name="hibernate.connection.pool_size"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout for debugging --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
  
          &lt;property name="hibernate.connection.provider_class"&gt;org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider&lt;/property&gt;
          &lt;property name="hibernate.c3p0.max_size"&gt;100&lt;/property&gt;
          &lt;property name="hibernate.c3p0.min_size"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.c3p0.idle_test_period"&gt;30&lt;/property&gt;
  
          &lt;mapping class="com.pirzada.domain.User"/&gt;
          &lt;!-- DB schema will be updated if needed --&gt;
          &lt;!-- &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt; --&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>UserDAOImpl</strong></p>
  
  <pre><code>/*@Transactional is needed so that a Hibernate transaction is set up, otherwise updates won't have an affect*/
  @Transactional
  public class UserDAOImpl implements UserDAO {
  
      // So Spring can inject the session factory
      protected SessionFactory sessionFactory;
      public void setSessionFactory(SessionFactory value) {
          sessionFactory = value;
      }
  
      // Shortcut for sessionFactory.getCurrentSession()
      protected Session session() {
          return sessionFactory.getCurrentSession();
      }
  
      @Override
      public void saveOrUpdateUser(User user) {
          session().saveOrUpdate(user);
      }
  }
  </code></pre>
  
  <p><strong>maven</strong></p>
  
  <pre><code>&lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.10&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- javaee-api 6 dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
              &lt;version&gt;6.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring framework dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;3.1.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.struts&lt;/groupId&gt;
              &lt;artifactId&gt;struts2-spring-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.3.4.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- struts2 dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.struts&lt;/groupId&gt;
              &lt;artifactId&gt;struts2-core&lt;/artifactId&gt;
              &lt;version&gt;2.3.4.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- sl4j dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.17&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${slf4j.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;${slf4j.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.core.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;4.3.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.core.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- JDBC dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.21&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  </code></pre>