Monday, March 25, 2013

Spring custom namespace

The dynamic service activator that we created has a very clumsy definition.

it'd be nice if it's something like this


To do that we need to create a custom namespace for spring and we can have our bean definition under beans. I found this blog by carlo scarioni detailing how to do that.  Following it, I was able to create my namespace and put in under beans.  However, it is not added to the application context.

For that, I followed Spring's doc, all i have to do is
  1. the xsd needs to import spring beans and the element needs to extend beans:identifiedType.
  2. the bean definition parser should extends AbstractSingleBeanDefinitionParser instead.
everything else is the same.  but now you'll have id attribute in your bean definition (from beans:identifiedType)!  and it will be added to the application context.

 Another cool thing we can use is to use spring tool so you can verify the bean ref is of a certain class.  Import the following namespace and add the annotation to your attribute.

<xs:import namespace="http://www.springframework.org/schema/tool" />


below is the gist we used to parse our custom tag. note that it set the scope to prototype at the end and it also shows you how to pass an array of beans.