java 找不到类 org.springframework.web.context.ContextLoaderListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17768480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:55:53  来源:igfitidea点击:

class org.springframework.web.context.ContextLoaderListener not found

javaspringmaventomcatspring-mvc

提问by Arun

When I am creating a new Spring MVC project using eclipse and maven, it throws the following error when I do a run as maven build(goal is tomcat:run). I did the build after clean and install.

当我使用 eclipse 和 maven 创建一个新的 Spring MVC 项目时,当我作为 maven build 运行时它会抛出以下错误(目标是 tomcat:run)。我在清理和安装后进行了构建。

SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

严重:配置 org.springframework.web.context.ContextLoaderListener 类的应用程序侦听器时出错

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

  1. The other solutions for this problem are to add the maven dependencies in deployment assembly from the project properties.
  2. But, the deployment assembly option is not present when I right click the properties.
  3. I also tried adding spring.jar manually.
  4. The class org.springframework.web.context.ContextLoaderListener is present in the maven dependency and still the error is thrown.
  1. 此问题的其他解决方案是从项目属性中在部署程序集中添加 maven 依赖项。
  2. 但是,当我右键单击属性时,部署程序集选项不存在。
  3. 我也尝试手动添加 spring.jar。
  4. 类 org.springframework.web.context.ContextLoaderListener 存在于 maven 依赖项中,但仍然抛出错误。

pom.xml

pom.xml

My pom.xml is as follows:

我的 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>com.org</groupId>
    <artifactId>Remindem</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Remindem</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.8.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.0.0.Beta2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.5.RELEASE</version>
            <scope>provided</scope>

        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
            <scope>provided</scope>
        </dependency>


        <!-- Spring MVC framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>2.5.6</version>
            <scope>provided</scope>
        </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> -->

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-api</artifactId>
            <version>7.0.19</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <url>http://localhost:8080/Remindem</url>
        </configuration>
      </plugin>
        </plugins>
    </build>
</project>

web.xml

网页.xml

My web.xml is as follow:

我的 web.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>Remindem</display-name>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>

     <servlet>
        <servlet-name>svn</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/SpringAppServlet.xml               
            </param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
</servlet> 
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/SpringAppServlet.xml
    </param-value>
</context-param> 


 <servlet-mapping>
    <servlet-name>svn</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

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

</web-app>

回答by gerrytan

This is because you mark your Spring dependencies with <scope>provided</scope>on your maven pom.xml and you did not include the actual jars on the classpath when running the program. There are two possible solutions:

这是因为您<scope>provided</scope>在 maven pom.xml 上标记了 Spring 依赖项,并且在运行程序时没有在类路径中包含实际的 jar。有两种可能的解决方案:

  1. Change the dependency scope into compile
  2. Include the spring jars with matching version to your classpath when running the application
  1. 将依赖范围改为compile
  2. 运行应用程序时,将具有匹配版本的 spring jar 包含到您的类路径中