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

引用地址:http://www.cnblogs.com/icewee/articles/3703491.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值