【问题标题】:Cannot find class [org.apache.commons.dbcp.BasicDataSource]找不到类 [org.apache.commons.dbcp.BasicDataSource]
【发布时间】:2011-11-29 11:03:27
【问题描述】:

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

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 文件。

【问题讨论】:

  • 在 STS / eclipse 类路径中包含 jars 是不够的 - 它们应该在 web-inf\lib 文件夹中 - 如果您尝试将其作为 webapp 运行。
  • 对于 STS,我真的不知道 web-inf/lib 路径在哪里。我查看了 web app 目录,没有 web-inf/lib 文件夹。

标签: hibernate spring-mvc sts-springsourcetoolsuite


【解决方案1】:

请确保 commons-dbcp-1.4.jar 文件在您的 lib 文件夹中。你应该在eclipse中复制过去。

看这张图片

【讨论】:

  • 我必须添加 common-pools.jar 和 spring.jar。此外,我必须添加一个 web-inf/lib 文件夹并将其拉入。我认为 STS 会自动创建,但它似乎没有这样做。
  • 所有 jars 都应该在那里,但对于您的问题 org.apache.commons.dbcp.BasicDataSource 位于 commons-dbcp-1.x.jar 内
  • 对于那些使用maven的人:commons-dbcpcommons-dbcp1.4
  • 有一个类似的问题 - 原因:我使用的是 20030825.183949。我把它改成 1.4 并且我被排序了。
【解决方案2】:

对我来说,仅仅将 commons-dbcp-1.4.jar 添加到我的类路径是行不通的。还必须添加 commons-pool-1.6.jar。 这个post 描述了原因。那个帖子里也有下载链接。

【讨论】:

    【解决方案3】:

    从eclipse做了一个干净的tomcat目录,不用下载jar文件就可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2013-12-31
      • 2020-10-15
      • 1970-01-01
      相关资源
      最近更新 更多