2011-10-02 26 views
14

我在嘗試啓動Spring MVC hibernate應用程序時遇到此異常。找不到類[org.apache.commons.dbcp.BasicDataSource]

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

+2

只有在STS/eclipse classpath中有jar文件是不夠的 - 它們應該放在web-inf \ lib文件夾中 - 如果你試圖將它作爲webapp運行。 – gkamal

+0

對於STS,我不知道web-inf/lib路徑在哪裏。我查看了Web應用程序目錄,並且沒有web-inf/lib文件夾。 – djoshi

回答

21

請確保commons-dbcp-1.4.jar文件位於lib文件夾內。你應該在eclipse中複製它。

看到該圖片

enter image description here

+0

我不得不添加common-pools.jar和spring.jar。另外,我必須添加一個web-inf/lib文件夾並將其放入。我認爲STS會自動創建該自動創建,但似乎並沒有這樣做。 – djoshi

+0

所有罐子應該是有的,但對位於公地DBCP-1.x.jar –

+5

內部對於那些誰用maven你的問題org.apache.commons.dbcp.BasicDataSource: \t \t 公地DBCP \t \t 公地DBCP \t \t 1.4 \t

0

的tomcat做一個乾淨的目錄從Eclipse和它的工作,而無需下載jar文件

3

對於我來說,只是增加公地DBCP-1.4.jar到我的類路徑沒有工作。必須添加commons-pool-1.6.jar以及。 這個post描述了原因。該帖子中還有一個下載鏈接。

相關問題