Thursday 28 July 2011

Spring NamespaceHandlers

Spring's XML schema-based configuration is convenient and can reduce developer workload, but often at the expense of a thorough understanding of what is happening under the covers.  The newer custom tags wield a lot of power, so it's worth understanding exactly how they work.

A XML schema definition is held in a .xsd file and can be included in the XML configuration file, see Referencing the schemas.

Each schema has a corresponding NamespaceHandler.  For example, the tx schema, i.e. the <tx:blah> tags are handled by the TxNamespaceHandler.  If you look in TxNamespaceHandler.java you will see that it registers a parser object for each tag in the tx schema.

The remaining piece of magic is the mapping between schema and NamespaceHandler.  This can be found in the spring.handlers file inside the associated jar's META-INF directory.  See Registering the handler and the schema.  In the case of the tx schema this file resides in the META-INF directory in org.springframework.transaction-3.0.x.RELEASE.jar.

No comments:

Post a Comment