首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring ContextLoaderListener 配置缺失導致啟動失敗:Maven 依賴修復指南

     編輯:Java 綜合教學

錯誤現象

嚴重: 配置類 org.springframework.web.context.ContextLoaderListener 初始化失敗
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

典型原因:web.xml 宣告了 ContextLoaderListener,但專案缺少 spring-web 模組依賴,或 Maven 依賴解析異常導致 JAR 未納入 WEB-INF/lib。

web.xml 典型配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

Maven 依賴修復

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.30</version> <!-- 或統一由 Spring BOM 管理版本 -->
</dependency>

關鍵點:ContextLoaderListener 位於 spring-web 模組,而非 spring-context。

Eclipse/Maven 專案同步檢查清單

  1. Maven → Update Project:右鍵專案 → Maven → Update Project → ☑ Force Update
  2. Deployment Assembly:Properties → Deployment Assembly → 確認 Maven Dependencies 已映射至 WEB-INF/lib
  3. 伺服器發布:Servers 視圖 → 右鍵伺服器 → Clean → Clean Tomcat Work Directory → Republish
  4. 本地倉庫完整性:刪除 ~/.m2/repository/org/springframework/spring-web/ 重新下載

Spring 版本對應表

進階:手動添加 JAR 到構建路徑 (不推薦,應修復 Maven)

若臨時需驗證:

  1. 從 ~/.m2/repository/org/springframework/spring-web/5.3.30/spring-web-5.3.30.jar 複製
  2. 貼至 WebContent/WEB-INF/lib/
  3. 右鍵專案 → Build Path → Configure Build Path → Add JARs → 選擇該 JAR

警告:此方式繞過 Maven 管理,升級/遞移依賴易失效,僅供臨時排查。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
Spring 版本spring-web 坐標最低 JDK
5.3.xorg.springframework:spring-web:5.3.30JDK 8+
6.0.xorg.springframework:spring-web:6.0.13JDK 17+
Spring Boot 2.7透過 spring-boot-starter-web 遞移JDK 8+
Spring Boot 3.x透過 spring-boot-starter-web 遞移JDK 17+