How to achieve conditional resource import in a Spring XML context?

Posted by Boris Terzic on Stack Overflow See other posts from Stack Overflow or by Boris Terzic
Published on 2010-06-14T07:37:17Z Indexed on 2010/06/14 9:12 UTC
Read the original article Hit count: 849

Filed under:
|
|
|

What I would like to achieve is the ability to "dynamically" (i.e. based on a property defined in a configuration file) enable/disable the importing of a child Spring XML context.

I imagine something like:

<import condition="some.property.name" resource="some-context.xml"/>

Where the property is resolved (to a boolean) and when true the context is imported, otherwise it isn't.

Some of my research so far:

  • Writing a custom NamespaceHandler (and related classes) so I can register my own custom element in my own namespace. For example: <myns:import condition="some.property.name" resource="some-context.xml"/>

    The problem with this approach is that I do not want to replicate the entire resource importing logic from Spring and it isn't obvious to me what I need to delegate to to do this.

  • Overriding DefaultBeanDefinitionDocumentReader to extend the behaviour of the "import" element parsing and interpretation (which happens there in the importBeanDefinitionResource method). However I'm not sure where I can register this extension.

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml