B.5. Registering the handler and the schema

Spring Framework

B.5. Registering the handler and the schema

We're done implementing the NamespaceHandler and the BeanDefinitionParser that will take care of parsing the custom XML Schema for us. We now have the following artifacts:

  • org.springframework.samples.xml.MyNamespaceHandler - namespace handler that will register one or more BeanDefinitionParser instances

  • org.springframework.samples.xml.SimpleDateFormatBeanDefinitionParser - used my the namespace handler to parse elements of type dateformat

  • org/springframework/samples/xml/myns.xsd - the actual schema that will be used in the Spring configuration files (note that this file needs to be on the classpath, alongside your namespace handler and parser classes as we'll see later on)

The last thing we need to do is to get the namespace ready for use by registering it in two special purpose properties files. These properties files are both placed in the META-INF directory and can, for example, be distributed alongside your binary classes in a JAR file. Spring will automatically pick up the new namespaces and handlers once it finds the properties files on the classpath.

B.5.1. META-INF/spring.handlers

The properties file called spring.handlers contains a mapping of XML Schema URIs to namespace handler classes. So for our example, we need to specify the following here:

http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler

B.5.2. META-INF/spring.schemas

The properties file called spring.schemas contains a mapping of XML Schema locations (referred to along with the schema declaration in XML files that use the schema as part of the xsi:schemaLocation attribute) to classpath resources. This file is needed to prevent Spring from having to use a default EntityResolver that requires Internet access to retrieve the schema file. If you specify the mapping in this properties file, Spring will search for the schema on the classpath (in this case 'myns.xsd' in the 'org.springframework.samples.xml' package):

http\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd