spring

Spring NamespaceHandler Example

This article will discuss one of the most common configuration problems in the spring framework i.e. ‘A namespace handler for one of the spring namespaces is not found’. Most of the times, this means that 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.
 
 
 
 
 
 
 

1. Introduction

1.1 Spring Framework

  • Spring is an open-source framework created to address the complexity of an enterprise application development
  • One of the chief advantages of the Spring framework is its layered architecture, which allows developers to be selective about which of its components they can use while providing a cohesive framework for J2EE application development
  • Spring framework provides support and integration to various technologies for e.g.:
    • Support for Transaction Management
    • Support for interaction with the different databases
    • Integration with the Object Relationship frameworks for e.g. Hibernate, iBatis etc
    • Support for Dependency Injection which means all the required dependencies will be resolved with the help of containers
    • Support for REST style webservices

1.2 Spring Namespaces

Spring Namespaces provides a good way to simplify the XML files used to describe the bean definitions of a Spring Application Context. It is fairly an old concept introduced with Spring 2.0 but deserves being reviewed once in a while.

Now let’s take a look and understand the most common configuration problems of this framework.

1.2.1 Security Namespace Handler

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 of this exception is straight-forward i.e. the spring-security-config dependency is missing from the classpath of the project. Add the below dependency to the project’s pom.xml:

<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 (i.e. in this case SecurityNamespaceHandler) in the classpath and ready to parse the elements in the security namespace.

Tip
Complete project tutorial for the Maven configuration can be found at this link.

1.2.2 AOP Namespace Handler

The same problem occurs when using the AOP namespace without having the necessary AOP (Aspect Object Programming) spring library in the 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-4.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
 
</beans>

The exact exception that occurs is:

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 to this exception is to add the spring-aop to the classpath of the project. Add the below dependency to the project’s pom.xml:

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

In this case, the AopNamespaceHandler will be present in the project’s classpath after adding the new dependency and will resolve the exception.

1.2.3 Transaction Namespace Handler

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>

The above bean definition will result in an exception if the right jar is not presented in the project’s 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. Add the below dependency to the project’s pom.xml:

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

Now, the correct namespace handler – namely TxNamespaceHandler will be presented in the project’s classpath allowing the declarative transaction management with both XML and annotations.

1.2.4 MVC Namespace Handler

Moving forward to the Spring’s MVC (i.e. Model, View, and Controller) namespace.

<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 below 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. Add the below dependency to the project’s pom.xml:

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

Adding the spring-mvc dependency to the pom.xml will add the MvcNamespaceHandler to the project’s classpath and it will allow the project to configure the MVC semantics using the namespace.

That’s all for this post. Happy Learning!!

2. Conclusion

In this tutorial we discussed the usual suspects for the ‘Unable to locate Spring NamespaceHandler for XML schema namespace’ problem and we provided solutions for each occurrence. That’s all for this tutorial and I hope this article served you whatever you were looking for.

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button