Spring XML Mapping

dturanski edited this page Oct 24, 2012 · 1 revision

SI Groovy DSL currently works by mapping the DSL configuration to an equivalent Spring XML application context. This approach allows us to reuse existing bean definition parsers where possible. Other advantages or this approach include:

  • The generated XML is logged as DEBUG output so you can get a better idea how the DSL works
  • Spring XML is fully supported using Groovy markup, allowing you to configure anything via native XML that is not directly supported by the DSL
  • The XML namespaces are fully documented

Spring XML Namespaces

When dropping down to XML, it is often necessary to add the appropriate namespace declarations for Spring and Spring Integration. This is easily accomplished using the namespaces method within the top level closure. For example:

builder.doWithSpringIntegration {
    namespaces('int-file')

    springXml {
    'int-file:inbound-channel-adapter'(
        id:'filesIn',
            channel='fileReadingChannel'
            filenameRegex:'test[0-9]+\.txt',
            directory='file:/someDir')

        'si:channel'(id:'fileReadingChannel')
    }
}

The example above points out some conventions used by the DSL to simplify namespace declaration. Note that the DSL declares the si namespace prefix for the core Spring Integration namespace. The int-file argument to the namespaces method generates the corresponding XML namespace declaration using existing Spring namespace conventions. For example:

<beans 
    xmlns:int-file="http://www.springframework.org/schema/integration/file"     
    xmlns="http://www.springframework.org/schema/beans"     
    xmlns:si="http://www.springframework.org/schema/integration"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/integration 
    http://www.springframework.org/schema/integration/spring-integration.xsd  
    http://www.springframework.org/schema/integration/file 
    http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
    ...
</beans>

Likewise, Core Spring Framework namespaces, for example amqp, task, jdbc are declared the same way. Any argument that starts with int- will generate an integration namespace. Otherwise, the standard core namespace URIs will be bound. As the name suggests, the namespaces method accepts a comma delimited list of namespace prefixes.

Note that DSL modules use the same convention internally. If you add a module to the IntegrationBuilder the corresponding integration namespace will be declared. The following code creates an IntegrationBuilder with the http module included. This will automatically declare the required int-http XML namespace.

 def builder = new IntegrationBuilder('http') 

DSL Named Parameters as XML Attributes

By default, DSL named parameters are treated as XML attributes for the corresponding elements. For example:

poll('poller','default':true,fixedDelay:1000)

Maps to an SI poller, defined in XML as follows:

<si:poller id="poller" default=true, fixed-delay=1000/>

The unnamed parameter 'poller' is the component's bean name. The named parameters in this case are output directly to XML, replacing camelCase names with hyphenated attribute names. Hyphenated names are also acceptable however they must be enclosed in (single) quotes as the Groovy parser treats '-' as a minus operator. Note also, in the example above 'default' is quoted since it is a Groovy key word.

Some named parameters may require additional handling by the DSL framework, especially those defined as closures. In such cases the XML mapping may be more complex.