Unable to locate Spring NamespaceHandler for XML schema namespace

1. 问题

本文将讨论Spring中最常见的配置问题 —— Spring的一个命名空间的名称空间处理程序没有找到。 大多数情况下,是由于一个特定的Spring的jar没有配置在classpath下,让我们列出多数可能出现的缺失配置以及导致的异常。

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.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.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的jar配置在classpath中(如:maven的pom.xml):

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

配置正确的名称空间处理程序 —— 在这种情况下classpath下的SecurityNamespaceHandler会解析安全名称空间中的元素。

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

发生在使用aop名称空间时,没有将相应的spring的jar配置在classpath下:

复制代码
<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-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.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]

解决方法与问题2类似,只需将spring-aop的jar配置calsspath下:

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

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-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 
</beans>
复制代码

 导致以下异常:

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]

解决方法,将事务的jar配置到classpath下:

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

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

下面是spring的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-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.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的jar配置在classpath中:

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

6.结论

最后,如果你是使用Eclipse来管理web服务器和部署,确保部署的组装部分项目是配置正确的 —— 即Maven的依赖,实际上是在部署时包含在类classpath中。

原文地址:https://www.cnblogs.com/toSeeMyDream/p/9443370.html