知識庫 / Spring / Spring Security RSS 訂閱

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

Spring Security
HongKong
6
03:09 PM · Dec 06 ,2025

1. 問題描述

本文將討論 Spring 中最常見的一種配置問題——一個 Spring 命名空間處理器未找到。 大多數情況下,這意味着缺少一個特定的 Spring JAR 包在類路徑上,讓我們來了解一下這些缺失的 Schema 以及每個 Schema 對應的缺失依賴。

2. Spring Security 安全命名空間

在實際應用中,最常見的問題是 Spring 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 命名空間 時,如果 classpath 上沒有配置必要的 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 JAR 包添加到項目的類路徑中:

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

在這種情況下,AopNamespaceHandler 將會在添加新依賴後出現在 classpath 上。

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>

現在,正確的 NamspaceHandler——即 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 服務器並進行部署,請確保 項目中的 Deployment Assembly 部分已正確配置——具體來説,Maven 依賴項在部署時確實包含在類路徑上。

本教程討論了“Unable to locate Spring NamespaceHandler for XML schema namespace”問題的常見原因,併為每個情況提供瞭解決方案。

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

發佈 評論

Some HTML is okay.