無法找到 Spring 命名空間處理程序,用於 XML 模式名稱空間

Spring Security
Remote
1
07:04 PM · Nov 29 ,2025

1. 問題描述

本文將討論 Spring 中最常見的一種配置問題——一個 Spring 命名空間處理程序未找到。 大多數情況下,這意味着某個特定的 Spring jar 沒有從 classpath 中包含 – 讓我們來了解這些缺失的 schema 以及每個 schema 對應的缺失依賴。

2. http://www.springframework.org/schema/security

在實際中,安全命名空間 不可用是遇到的最普遍的問題:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

</beans:beans>

這導致了以下異常:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 
Configuration problem: 
Unable to locate Spring NamespaceHandler for XML schema namespace 
[http://www.springframework.org/schema/security]
Offending resource: class path resource [securityConfig.xml]

解決方案很簡單 – 項目類路徑中缺少 spring-security-config 依賴:

<dependency> 
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-config</artifactId>
   <version>3.2.5.RELEASE</version>
</dependency>

這將使正確的命名空間處理程序 – 在這種情況下是 SecurityNamespaceHandler – 位於類路徑上,並準備好解析 security 命名空間中的元素。

完整的 Spring Security 設置的 Maven 配置可以在我之前的 Maven 教程中找到。

3. http://www.springframework.org/schema/aop

當使用 aop 命名空間時,如果沒有在類路徑上配置必要的 spring-aop 庫,就會出現相同的問題:

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

</beans>

確切的異常信息:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 
Configuration problem: 
Unable to locate Spring NamespaceHandler for XML schema namespace 
[http://www.springframework.org/schema/aop]
Offending resource: ServletContext resource [/WEB-INF/webConfig.xml]

解決方案與此類似——需要將 spring-aop 庫添加到項目的類路徑中:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>4.1.0.RELEASE</version>
</dependency>

在這種情況下,添加新的依賴後,AopNamespaceHandler 將會在類路徑上存在。

4. http://www.springframework.org/schema/tx

使用 事務命名空間——一個用於配置事務語義的,小但非常實用的命名空間:

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

</beans>

也會在右側 jar 未在類路徑上會導致異常:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 
Configuration problem: 
Unable to locate Spring NamespaceHandler for XML schema namespace
[http://www.springframework.org/schema/tx]
Offending resource: class path resource [daoConfig.xml]

此處缺少依賴項是 spring-tx:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.0.RELEASE</version>
</dependency>

現在,正確的 NamespaceHandler——即 TxNamespaceHandler——將會在類路徑上存在,從而允許使用 XML 和註解進行聲明式事務管理。

5. http://www.springframework.org/schema/mvc

繼續到 mvc 命名空間:

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

</beans>

缺少依賴將導致以下異常:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 
Configuration problem: 
Unable to locate Spring NamespaceHandler for XML schema namespace
[http://www.springframework.org/schema/mvc]
Offending resource: class path resource [webConfig.xml]

在這種情況下,缺少依賴是 spring-mvc:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.1.0.RELEASE</version>
</dependency>

將此項添加到 pom.xml 將向類路徑添加 MvcNamespaceHandler – 允許項目使用命名空間配置 MVC 語義。

6. 結論

最後,如果您使用 Eclipse 來管理 Web 服務器和部署 – 請確保 部署組裝部分 項目已正確配置 – 尤其是 Maven 依賴是否在部署時包含在類路徑中。

本教程討論了“無法找到 Spring NamespaceHandler 用於 XML 模式命名空間”問題的常見原因,並提供了每個情況的解決方案。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.