java.lang.ClassNotFoundException:org.apache.commons.dbcp.BasicDataSource

时间:2014-03-10 10:05:28

标签: java spring maven spring-mvc

我正在使用maven.Iam在spring MVC上做一个项目。在运行project.Iam时遇到错误java.lang.ClassNotFoundException:org.apache.commons.dbcp.BasicDataSource我包含了所有的依赖项,我认为..我的代码。等待你的回复

WEB.XML

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <servlet>
 <servlet-name>AccPerSpring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-context.xml</param-value>
 </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
    <servlet-mapping>
       <servlet-name>AccPerSpring</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>
 </web-app>

servlet的context.xml中

  <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"              xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"   xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <!-- Enable @Controller annotation support -->
   <mvc:annotation-driven />


   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/" />
         <property name="suffix" value=".jsp" />
   </bean>
  <context:component-scan base-package="com.gerrytan.pizzashop"/>
  <bean id="myDataSource" class=" org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
       <property name="username" value="root"/>
       <property name="password" value="kca@fnpl#12"/>
 </bean>

     <!-- Hibernate Session Factory -->
    <bean id="mySessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean>
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
     <value>com.gerrytan.pizzashop</value>
    </array>
   </property>
    <property name="hibernateProperties">
   <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
   </value>
   </property>
   </bean>

    <!-- Hibernate Transaction Manager -->
    <bean id="transactionManager"    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
   </bean>

   <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
   <!-- Activates annotation based transaction management -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
   </beans>

的pom.xml

     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>AccPerSpring</groupId>
     <artifactId>AccPerSpring</artifactId>
      <version>0.0.1-SNAPSHOT</version>
     <packaging>war</packaging>

     <properties>
     <spring.version>3.2.3.RELEASE</spring.version>
      <hibernate.version>4.2.2.Final</hibernate.version>
      </properties>

    <dependencies>
     <!-- Spring -->
         <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
        </dependency>
     <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-orm</artifactId>
     <version>${spring.version}</version>
   </dependency>

<!-- Hibernate -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>${hibernate.version}</version>
</dependency>

<!-- Java EE -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<!-- Others -->
<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.25</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
</dependency>

 </dependencies>

   <build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.1</version>
       <configuration>
        <source>1.7</source>
        <target>1.7</target>
       </configuration>
     </plugin>
     </plugins>
     </build>
  </project>

3 个答案:

答案 0 :(得分:6)

构建完成后,请检查您的war文件 - &gt; commons-dbcp-1.4.jar的WEB-INF / lib。如果它在类路径中可用,那么您的问题将得到解决。 还有一件事是,打开你的commons-dbcp-1.4.jar并检查特定的类(org.apache.commons.dbcp.BasicDataSource)是否可用?如果不是,你必须尝试其他版本的commons-dbcp-1.4.jar。

答案 1 :(得分:3)

当你有同一个类的冲突版本时,有时你会得到这些奇怪的异常。这可能是因为同一JAR的不同版本已经存在于应用程序服务器的类路径中,或者它是通过不同的依赖项包含的。

mvn dependency:tree是一个命令,您可以运行该命令以查看进入应用程序的所有依赖项,包括从其他JAR引入的依赖项。可能值得运行此操作来检查可能导致冲突的其他“dbcp”JAR。

还要检查您正在部署的应用程序服务器中的“lib”文件夹,以查看是否已存在任何“dbcp”JAR。如果是这种情况,那么将范围更改为“commons-dbcp”工件的“提供”应该可以解决问题。

答案 2 :(得分:1)

一个常见错误可能是导入类

  

org.apache.tomcat.dbcp.dbcp.BasicDataSource

而不是

  

org.apache.commons.dbcp.BasicDataSource

你的java代码中的

。即使在错误消息中,它也很容易被遗漏。