找不到类[org.apache.commons.dbcp.BasicDataSource]

时间:2011-10-02 16:09:14

标签: hibernate spring-mvc sts-springsourcetoolsuite

我在尝试启动我的Spring MVC hibernate应用程序时遇到此异常。

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)

我的/WEB-INF/spring/appServlet/mysql_persistence_info.xml是

  <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>

我在classapath中需要所有jar文件。 我正在使用STS。

我有commons-dbcp.jar和所有其余的jar文件。

3 个答案:

答案 0 :(得分:21)

请确保commons-dbcp-1.4.jar文件位于lib文件夹中。你应该在eclipse中复制它。

见图片

enter image description here

答案 1 :(得分:3)

对我来说,只是将 commons-dbcp-1.4.jar 添加到我的类路径中不起作用。不得不添加 commons-pool-1.6.jar 。 这个post描述了原因。该帖子中也有一个下载链接。

答案 2 :(得分:0)

从eclipse做了一个干净的tomcat目录,它没有下载jar文件