As mentioned
I stumbled over a few things while writing my
first NamespaceHandler
for Spring.
My very first experiments looked as if Spring simply was ignoring my namespace and I ended up having the XML parser complain about not finding my schema definition. It seemed to be some kind of infrastructural problem and not a code problem, so I tried to get the simple dateformat example from the docs working.
Unfortunately the example doesn't work without changes since the
four different places you have to specify the XML Namespace URI or
XSD location use two different root URIs
(http://www.mycompany.com/schema/myns
and
http://www.springframework.org/schema/myns
).
Bug reported.
After some futzing around I realized that my XSD file wasn't ending
up on Spring's CLASSPATH and thus Spring didn't find it. I would
have expected Spring to complain if I specify a location mapping
in META-INF/spring.schemas
that it cannot resolve.
Unfortunately it fails silently.
Writing the NamespaceHandler
is really simple, as is
writing a BeanDefinitionParser
, at least if you don't
need to do fancy things.
Not being familiar with Spring's internals I kept looking around
the BeanDefinitionBuilder
class for a way to provide an
id
for the bean under construction. Maybe I should
have looked closer or the documentation isn't that obvious. If you
extend AbstractBeanDefinitionParser
(maybe indirectly)
you set the bean's id by overriding resolveId
.
Up to here I was
extending AbstractSingleBeanDefinitionParser
because it
did all the things I needed.
Next step: I want to define a bean that has a parent bean.
Searching through BeanDefinitionBuilder
's API docs lead
to the childBeanDefinition
factory method, which
clearly is the way to go.
Unfortunately AbstractSingleBeanDefinitionParser
hasn't
been designed for my use case so I ended up writing my own
AbstractBeanDefinitionParser
subclass that is almost
identical to AbstractSingleBeanDefinitionParser
but
adds another protected getParentName(Element)
method
that will lead to the creation of a child bean if it returns
non-null (and neither getBeanClass
nor getBeanClassName
return non-null).
If anybody is interested in the code, you can find it here - Apache License 2.0.
path: /en/Java/Spring | #