tags:

views:

61

answers:

1

I'd like to be able to load spring context.xml files on-the-fly, so that they are wired with previously loaded contexts (meaning, in contextA.xml I can ref a bean defined in contextB.xml which was already loaded). I would like for existing beans to not be destroyed and then created whenever a context is added.

+2  A: 

It's easy enough to do, most of the BeanFactory and ApplicationContext implementations have the concept of a parent context.

BeanFactory newFactory = new XmlBeanFactory(xmlResource, yourParentBeanFactory);

// if you what an ApplicationContext

ApplicationContext newContext = new ClassPathXmlApplicationContext( new String[]{"newBeans.xml"}, parent);

see http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/xml/XmlBeanFactory.html

References inside the new context that can not be resolved within it's self are passed up the parent to and to it's parent.

Note you can get hold of the current application context by implementing ApplicationContextAware.

Gareth Davis
And without using parent?
IttayD
that would involve changing the space time continuum and crossing the streams at the same time. Giving the new context a parent is the way to do it, there isn't really any other option.
Gareth Davis