Maven + Spring +动态Web模块(Eclipse)= java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener

Vic*_*ana 5 eclipse spring maven

我的问题是,甚至设置"部署程序集"以包含maven依赖项,这使得我的类没有找到,我不知道还能做什么.

我只是注意到这个ContextLoaderListener类,因为其他类似乎包含在我的包中.

我的文件是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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.common</groupId>
    <artifactId>SpringMVC</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringMVC Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> 
            <version>3.8.1</version> <scope>test</scope> </dependency> -->

        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- Spring MVC framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- for compile only, your container should have this -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>SpringMVC</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.0-beta-1</version>
                <configuration></configuration>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

我的文件web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>
Run Code Online (Sandbox Code Playgroud)

Adr*_* Be 10

这个问题似乎是由于:

  1. 一些库版本不匹配(即java版本编译!= web应用服务器上的java版本).
  2. 部署期间不包括必需的Spring库.
  3. Maven原型用于设置项目

执行以下步骤以尝试解决此问题:

  • 在pom.xml中>如果你使用maven-compiler-plugin,请仔细检查这是否与所用库的版本匹配,如果没有改变它(即1.7不1.6).

    <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
      <source>1.7</source>
      <target>1.7</target>
     </configuration>
    </plugin>
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果您在上面的步骤中更改了库版本,则必须执行以下操作:右键单击Project> Maven> Update Project ...如果在此步骤中抛出错误,您可能会发现自己陷入困境.我有这个问题,无法解决它.我最终放弃了使用原型在Eclipse中创建我的"带有Maven的Spring MVC".我相信在我的情况下,使用原型搞砸了.

  • 右键单击项目>属性> Java构建路径> JRE系统库>编辑>选择正确的库版本

  • 右键单击项目>属性>项目构面>动态Web模块(应检查)>面板右侧的"运行时"选项卡>选择已创建的服务器(您之前必须已添加服务器).

  • 右键单击Project> Properties> Deployment Assembly> Add> Java Build Path Entries>选择Spring依赖项.这是在加载ContextLoaderListener时来自ClassNotFoundException注意:每次在eclipse中执行"Maven>更新项目"后,必须执行此步骤

  • 在部署之前,请记住清理项目:项目>清理...>选择项目>清理

  • 部署你的项目并看到它不会破坏......希望如此

在我的情况下,我不得不重新启动一个新项目,而不使用maven原型来开始.我使用了这些指南http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/.我仍然抛出一个错误,但是通过Deployment Assembly添加Spring依赖项(如上所述)修复了问题.