FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
Spring 容器啟動時,ContextLoaderListener 默認從 /WEB-INF/applicationContext.xml 加載配置,但實際部署路徑中找不到該文件。
WEB-INF/applicationContext.xml 物理存在。contextConfigLocation 參數是否正確?<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value> <!-- 默認值,可省略 -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
src/main/webapp/WEB-INF 下資源複製到 target/.../WEB-INF?檢查 pom.xml <resources> 配置。mvn clean package,刪除服務器部署目錄重新發布。// 在 ServletContextListener 中打印真實路徑
public class DebugListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
String path = sce.getServletContext().getRealPath("/WEB-INF/applicationContext.xml");
System.out.println("Real path: " + path); // null 表示未部署
}
}
90% 為**構建/部署配置導致文件未進入 WAR**,而非代碼邏輯錯誤。依次檢查:源文件位置 → 打包配置 → 服務器發布配置 → Clean Rebuild。