Unable to locate Spring NamespaceHandler for XML schema namespace

原文地址:[url]http://www.baeldung.com/unable-to-locate-spring-namespacehandler-for-xml-schema-namespace[/url]
1. The Problem

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.
2. http://www.springframework.org/schema/security

The security namespace not being available is by far the most widely encountered problem in practice:
?
1
2
3
4
5
6
7
8
9
10
11

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

Which leads to the following exception:
?
1
2
3
4
5

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:
?
1
2
3
4
5

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.4.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.

The complete Maven configuration for a full Spring Security setup can be found in my previous Maven tutorial.
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:
?
1
2
3
4
5
6
7
8
9
10
11

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

The exact exception:
?
1
2
3
4
5

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:
?
1
2
3
4
5

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

In this case, the AopNamespaceHandler will be present on the classpath after adding the new dependency.
4. http://www.springframework.org/schema/tx

Using the transaction namespace – a small but very useful namespace for the configuration of the transactional semantics:
?
1
2
3
4
5
6
7
8
9
10
11

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

will also result in an exception if the right jar is not on the classpath:
?
1
2
3
4
5

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:
?
1
2
3
4
5

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.5.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.
5. http://www.springframework.org/schema/mvc

Moving forward to the mvc namespace:
?
1
2
3
4
5
6
7
8
9
10
11

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

The missing dependency will lead to the following exception:
?
1
2
3
4
5

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:
?
1
2
3
4
5

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.5.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.
6. Conclusion

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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值