Unable to locate Spring NamespaceHandler for XML schema namespace

1.问题

本文将讨论Spring中最常见的配置问题之一 a namespace handler for one of the Spring namespaces is not found。大多数情况下,这意味着类路径中缺少一个特定的Spring jar。让我们来看看可能缺失的Schemas是什么,以及每个缺少的依赖是什么。

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

到目前为止,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命名空间中的元素。

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

使用aop命名空间时没有在类路径上有必要的aop spring库时会出现同样的问题:

<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将出现在类路径中。

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语义。

六,总结

最后,如果您使用Eclipse来管理Web服务器并进行部署 - 请确保正确配置项目的Deployment Assembly部分 -即在部署时,Maven依赖项实际上包含在类路径中。

本教程讨论了“无法找到Spring NamespaceHandler for XML schema namespace”问题的常见疑问,并为每次出现提供了解决方案。

原文地址:https://www.cnblogs.com/xjknight/p/10891130.html