Maven + Spring + 动态 Web 模块 (Eclipse) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10046654/
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
Maven + Spring + Dynamic Web Module ( Eclipse ) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
提问by Victor Viana
My problem is that even set the "Deployment Assembly" to include the maven dependencies, this giving that my class is not being found, I do not know what else to do.
我的问题是,即使将“部署程序集”设置为包含 Maven 依赖项,这也导致找不到我的类,我不知道还能做什么。
And I'm just noticing that with the ContextLoaderListener
class since other classes seem to be included in my package.
我只是注意到这个ContextLoaderListener
类,因为其他类似乎包含在我的包中。
My file pom.xml
我的文件 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>
My file web.xml
我的文件 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>
回答by Adrien Be
This problem seems to be either due to:
这个问题似乎是由于:
- Some library version mismatch (ie. java version to compile != java version on web app server).
- Required Spring library not included during deployment.
- Maven archetypes used to setup the project
- 某些库版本不匹配(即要编译的 Java 版本!= Web 应用服务器上的 Java 版本)。
- 部署期间未包含所需的 Spring 库。
- 用于设置项目的 Maven 原型
Perform these steps to try fix this issue:
执行以下步骤以尝试解决此问题:
In pom.xml > if you use maven-compiler-plugin, double check this is matching the version of the library used, if not change it (ie. 1.7 NOT 1.6).
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin>
If you changed the library version in the above step, you then must do: Right Click on Project > Maven > Update Project... If an error is thrown during this step, you may find yourself stuck for good. I had this issue and could not resolve it. I ended up giving up creating my "Spring MVC with Maven" in Eclipse using archetypes. I believe using archetypes messed things up..in my case.
Right Click on Project > Properties > Java Build Path > JRE System Library > Edit > choose correct library version
Right Click on Project > Properties > Project Facets > Dynamic Web module (should be checked) > Runtimes tab on the right side of the panel > choose the server created (you must have added a server previously).
Right Click on Project > Properties > Deployment Assembly > Add > Java Build Path Entries > select the Spring dependencies. This is from ClassNotFoundException while loading ContextLoaderListenerNote: You must perform this step every time after doing a "Maven > Update Project" in eclipse
Remember to clean your project before to deploy it: Project > Clean ... > select project > clean
Deploy your project and see it NOT break... hopefully
在 pom.xml > 如果您使用 maven-compiler-plugin,请仔细检查这是否与所使用的库版本匹配,如果没有更改(即 1.7 NOT 1.6)。
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin>
如果您在上述步骤中更改了库版本,则必须执行以下操作: 右键单击项目 > Maven > 更新项目... 如果在此步骤中抛出错误,您可能会发现自己陷入困境。我遇到了这个问题,无法解决。我最终放弃了在 Eclipse 中使用原型创建我的“Spring MVC with Maven”。我相信使用原型会把事情搞砸……就我而言。
右键单击 Project > Properties > Java Build Path > JRE System Library > Edit > 选择正确的库版本
右键单击项目> 属性> 项目构面> 动态Web 模块(应选中)> 面板右侧的运行时选项卡> 选择创建的服务器(您之前必须添加一个服务器)。
右键单击 Project > Properties > Deployment Assembly > Add > Java Build Path Entries > 选择 Spring 依赖项。这是在加载 ContextLoaderListener 时 ClassNotFoundException注意:每次在 Eclipse 中执行“Maven > Update Project”后都必须执行此步骤
记得在部署之前清理你的项目:项目 > 清理 ... > 选择项目 > 清理
部署你的项目,看看它不会中断......希望
In my case, I had to restart a new project withoutusing maven archetypes to get going. I used these guidelines http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/. I still had an error thrown, but adding the Spring dependencies via Deployment Assembly (as described above) fixed things up.
就我而言,我不得不在不使用 maven 原型的情况下重新启动一个新项目。我使用了这些指南http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/。我仍然抛出了一个错误,但是通过部署程序集添加 Spring 依赖项(如上所述)修复了问题。
回答by Cam
I had this same problem. Simple solution is to right click the top level project -> Properties -> Deployment Assembly and include the 'Maven Dependencies'. In my case this is where my problem was. The class DispatcherServlet was not being included in the war file, therefore it was not found by Tomcat when I deployed it to that server.
我有同样的问题。简单的解决方案是右键单击顶级项目 -> 属性 -> 部署程序集并包含“Maven 依赖项”。就我而言,这就是我的问题所在。类 DispatcherServlet 未包含在 war 文件中,因此当我将它部署到该服务器时,Tomcat 找不到它。
回答by Badal
I had similar issue and followed these steps to fix it.
我有类似的问题,并按照这些步骤来解决它。
- Open Server tab in eclipse. Right click on your server.
- Clean (Tomcat) Work directory
- Clean (this is different than Work directory clean up which you could see in right click context)
- 在 Eclipse 中打开服务器选项卡。右键单击您的服务器。
- 清理(Tomcat)工作目录
- 清理(这与您可以在右键单击上下文中看到的工作目录清理不同)