|
From: Stefan B. <ste...@fr...> - 2007-08-27 13:17:03
|
Hi,
recently I wanted to write a BeanDefinitionParser that creates child
beans. All the reasons to use AbstractSingleBeanDefinitionParser
apply in my case, but there is no way to create child beans since the
class doesn't account for it.
I ended up writing my own AbstractBeanDefinitionParser subclass that
was a 1:1 copy of AbstractSingleBeanDefinitionParser with just a few
changes:
(1) I introduced a protected String getParentName(Element) method.
(2) parseInternal uses the above method if getBeanClass and
getBeanClassName both return null. It Invokes
BeanDefinitionBuilder.childBeanDefinition(parentName);
if getParentName wasn't null (and I certainly moved the Assert
statement so that now one of the three methods has to return
non-null).
(3) Changed
builder.setSingleton(parserContext.getContainingBeanDefinition().isSingleton());
to
builder.setScope(parserContext.getContainingBeanDefinition().getScope());
I'm not too sure about (3) but it seemed to be the right thing to do.
Of course I don't like repeating the code of parseInternal in my own
class. Is there a simpler way? Or alternatively could
AbstractSingleBeanDefinitionParser be opened up (my pretty simple
patch would be available if you think you want it).
Cheers
Stefan
--
http://stefan.samaflost.de/
|