Unable to Locate Spring NamespaceHandler for XML Schema Namespace – 无法找到用于XML模式名称空间的Spring NamespaceHandler

最后修改: 2013年 7月 13日

中文/混合/英文(键盘快捷键:t)

1. The Problem

1.问题

This article will discuss one of the most common configuration problems in Spring – a namespace handler for one of the Spring namespaces is not found. Most of the time, this means one particular Spring jar is missing from the classpath – so let’s go over what these missing schemas might be, and what the missing dependency is for each one.

本文将讨论Spring中最常见的配置问题之一–未找到Spring某个命名空间的命名空间处理器。大多数时候,这意味着classpath中缺少一个特定的Spring jar–所以我们来看看这些缺少的模式可能是什么,以及每个模式缺少的依赖是什么。

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

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

The security namespace not being available is by far the most widely encountered problem in practice:

安全命名空间不可用是迄今为止在实践中最广泛遇到的问题。

<?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>

Which leads to the following exception:

这导致了以下的例外。

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]

The solution is straightforward – the spring-security-config dependency is missing from the classpath of the project:

解决方案很简单–项目的classpath中缺少spring-security-config依赖项。

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

This will put the correct namespace handler – in this case SecurityNamespaceHandler – on the classpath and ready to parse the elements in the security namespace.

这将把正确的命名空间处理程序–在这里是SecurityNamespaceHandler–放在classpath上,准备解析security命名空间的元素。

The complete Maven configuration for a full Spring Security setup can be found in my previous Maven tutorial.

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

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

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

The same problem occurs when using the aop namespace without having the necessary aop spring library on the classpath:

当使用aop命名空间而classpath上没有必要的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>

The exact exception:

确切的例外。

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]

The solution is similar – the spring-aop jar needs to be added to the classpath of the project:

解决办法是类似的–spring-aop jar需要被添加到项目的classpath中。

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

In this case, the AopNamespaceHandler will be present on the classpath after adding the new dependency.

在这种情况下,AopNamespaceHandler将在添加新的依赖性后出现在classpath上。

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

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

Using the transaction namespace – a small but very useful namespace for the configuration of the transactional semantics:

使用事务命名空间–一个小型但非常有用的命名空间,用于配置事务语义。

<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>

will also result in an exception if the right jar is not on the classpath:

如果正确的jar不在classpath上,也将导致一个异常。

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]

The missing dependency here is spring-tx:

这里缺少的依赖关系是spring-tx

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

Now, the right NamspaceHandler – namely TxNamespaceHandler – will be present on the classpath allowing the declarative transaction management with both XML and annotations.

现在,正确的NamspaceHandler–即TxNamespaceHandler–将出现在classpath上,允许用XML和注释进行声明式事务管理。

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

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

Moving forward to the mvc namespace:

前进到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>

The missing dependency will lead to the following exception:

缺少的依赖关系将导致以下异常。

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]

In this case, the missing dependency is spring-mvc:

在这种情况下,缺少的依赖是spring-mvc

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

Adding this to the pom.xml will add the MvcNamespaceHandler to the classpath – allowing the project to configure MVC semantics using the namespace.

将此添加到pom.xml将把MvcNamespaceHandler添加到classpath中 – 允许项目使用命名空间来配置MVC语义。

6. Conclusion

6.结论

Finally, if you’re using Eclipse to manage the web server and deploy – make sure that the Deployment Assembly section of the project is correctly configured – namely that the Maven dependencies actually are included on the classpath at deployment time.

最后,如果您使用Eclipse管理Web服务器并进行部署–请确保项目的部署组件部分配置正确–即在部署时Maven依赖项确实包含在classpath中。

This tutorial discussed the usual suspects for the “Unable to locate Spring NamespaceHandler for XML schema namespace” problem and provided solutions for each occurrence.

本教程讨论了 “无法定位XML模式命名空间的Spring NamespaceHandler “问题的常见嫌疑人,并为每一次出现的问题提供了解决方案。