14

I am getting this exception when trying to start my spring MVC hibernate app.

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/appServlet/mysql_persistence_info.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1235)

My /WEB-INF/spring/appServlet/mysql_persistence_info.xml is

  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/MyrDB"/>
    <property name="username" value="user"/>
    <property name="password" value="user"/>
    <!-- connection pooling details -->
    <property name="initialSize" value="1"/>
    <property name="maxActive" value="5"/>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    <property name="annotatedClasses">
    <list>
    <!--   all the annotation entity classes -->
    </list>
    </property>
    </bean>
    </beans>

I have all jar files required in classapath. I am using STS.

I have commons-dbcp.jar and all the rest of the jar files too.

Majid
  • 13,853
  • 15
  • 77
  • 113
djoshi
  • 363
  • 3
  • 7
  • 18
  • 2
    It is not enough to have the jars in STS / eclipse classpath - they should be in the web-inf\lib folder - if you are trying to run it as a webapp. – gkamal Oct 02 '11 at 17:10
  • With STS I don't know really know where the web-inf/lib path is. I looked in the web app directory and there is no web-inf/lib folder. – djoshi Oct 02 '11 at 19:37

3 Answers3

21

Please make sure that commons-dbcp-1.4.jar file is inside of your lib folder. you should copy past it in eclipse.

See this image

enter image description here

danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • I had to add common-pools.jar and spring.jar. In addition I had to add a web-inf/lib folder and pull it in. I would think STS would have that auto created but it does not seem it was doing it. – djoshi Oct 02 '11 at 20:03
  • All jars supposed to be there, but for your question org.apache.commons.dbcp.BasicDataSource located inside of commons-dbcp-1.x.jar – danny.lesnik Oct 02 '11 at 20:09
  • 5
    For those who use maven : commons-dbcp commons-dbcp 1.4 – Jean-Charles Jun 18 '12 at 07:09
  • had a similar issue - reason: I was using 20030825.183949. I changed it to 1.4 and I was sorted. – Adriano Sep 27 '12 at 12:04
4

For me, just adding commons-dbcp-1.4.jar to my classpath did not work. Had to add commons-pool-1.6.jar as well. This post describes the reason. There is a download link too in that post.

Community
  • 1
  • 1
Saad
  • 93
  • 2
  • 9
0

did a clean directory of tomcat from eclipse and it worked without downloading the jar file

Reda
  • 1,277
  • 1
  • 13
  • 27