在 web.xml 中配置 DispatcherServlet 時,contextConfigLocation 使用 classpath*:config/application.xml,許多開發者對 classpath* 的具體語義及路徑解析規則存在誤解。
classpath* vs classpath: 核心區別classpath:config/application.xml:僅加載 第一個 匹配的資源(通常是第一個 JAR 或源碼目錄下的文件)。classpath*:config/application.xml:加載 所有 Classpath 下匹配的資源並合併(適用於多模塊項目,各模塊各自有配置文件)。classpath 具體指向的路徑編譯後的 類輸出目錄(即 .class 文件所在根目錄)。常見情況:
target/classes、target/test-classes、依賴 JAR 包內部根目錄。bin 或 target/classes)。src/main/resources 編譯後內容會複製到輸出目錄根路徑。No mapping found for HTTP request with URI [/SpringMVC1/hello] 排查步驟Loading XML bean definitions from class path resource [config/application.xml]。application.xml 掃描配置: 是否包含 <context:component-scan base-package="your.controller.package"/>。@RequestMapping("/hello") 或類級別 @RequestMapping("/xxx")。/,需配置 <mvc:default-servlet-handler/> 或 <mvc:resources/> 放行靜態資源。/SpringMVC1,URL 完整為 https://host:port/SpringMVC1/hello。src/main/resources/config/,Maven 自動複製到 target/classes/config/,Classpath 即可直接訪問。-Dlog4j.configuration=file:log4j.properties 觀察 Spring 容器啟動日誌,快速定位配置加載問題。