29

In my Spring 3.1 application, I sometime need to change the default behavior of some of the Spring namespaces in my context files. To do that, I create custom classes that implement some interfaces or that extend the default classes Spring uses.

But I find it hard to know exactly what are those classes that Spring uses behind its namespaces! What is the steps required to find them?

For example, the security namespace :

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://www.springframework.org/schema/security 
                           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

and something like :

<sec:http>
    ...
    <sec:logout />
</sec:http>

How do I find what classes are used by the "<sec:logout />" namespace? I don't find the information by looking at http://www.springframework.org/schema/security/spring-security-3.1.xsd !

Where should I look?

Ritesh
  • 7,472
  • 2
  • 39
  • 43
electrotype
  • 8,342
  • 11
  • 59
  • 96

4 Answers4

50

Every Spring namespace has an associated NamespaceHandler implementation. The namespace schemas are mapped to schema files inside Spring JARs in various spring.schemas files (see also Spring DI applicationContext.xml how exactly is xsi:schemaLocation used?).

The XML schema namespaces are also mapped to handler classes in spring.handlers files (several as each Spring JAR might introduce different namespaces). For your convenience here is a list of most common namespaces:

Spring core

Spring Security

Spring integration

If you browse to the source of each of these classes you will quickly discover various BeanDefinitionParser implementations responsible for parsing actual XML definitions.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 1
    Thanks Tomasz! This, mixed with some of the tricks by ericacm helped me a alot. In eclipse (for those using it), opening the "Type hierarchy" for NamespaceHandler and BeanDefinitionParser also helps. – electrotype Jun 24 '12 at 19:58
  • 1
    For Spring Batch: `batch`-[CoreNamespaceHandler](http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/configuration/xml/CoreNamespaceHandler.html) – Abdull Aug 24 '13 at 19:00
  • Hazelcast `hz` [HazelcastNamespaceHandler](http://docs.hazelcast.org/docs/3.6-EA/javadoc/com/hazelcast/spring/HazelcastNamespaceHandler.html) – Kaszaq Nov 13 '15 at 19:42
2

Almost all of them are named *BeanDefinitionParser. First step is, using your browser, bring up the Spring JavaDocs and hit <Ctrl>-F (or <Command>-F). Search for BeanDefinitionParser. Of the classes that match, one or two will, by their names, look like they handle the namespace elements you are researching. It may take a bit of looking at the source code to those classes but you'll ultimately find what you want.

Alternatively, in your IDE, you can browse to BeanDefinitionParser.java and then do "Find Usages" (IntelliJ) or "Type Hierarchy" (Eclipse) to find all of the implementers of that interface. There are a handful of classes that don't follow the *BeanDefinitionParser naming convention. Your IDE will give you a comprehensive list.

I think that Spring should actually document, in the Namespace section of their reference docs, the names of the classes that handle each element.

sourcedelica
  • 23,940
  • 7
  • 66
  • 74
1

Every XML namespace is parsed by its NamespaceHandler. The handler class for security namespace is SecurityNamespaceHandler, which is specified in spring.handlers file inside META-INF of spring-security-config-XXX.jar file.

The http element is parsed by HttpSecurityBeanDefinitionParser and its child element logout is parsed by org.springframework.security.config.http.LogoutBeanDefinitionParser(package-protected).

Also see notes in Coding a NamespaceHandler and advice in What beans are registered by the spring security namespace? if you are planning to customize it.

Community
  • 1
  • 1
Ritesh
  • 7,472
  • 2
  • 39
  • 43
0

In the case of Spring Security, it's well documented: see Appendix B. The Security Namespace

See also this blog post: BEHIND THE SPRING SECURITY NAMESPACE

jbbarquero
  • 2,842
  • 2
  • 19
  • 17