spring:即时添加xml上下文?

Itt*_*ayD 5 java spring

我希望能够即时加载spring context.xml文件,以便它们与先前加载的上下文连接(意思是,在contextA.xml中我可以引用已加载的contextB.xml中定义的bean) ).我希望不会销毁现有的bean,然后在添加上下文时创建.

Gar*_*vis 4

这很容易做到,大多数 BeanFactory 和 ApplicationContext 实现都有父上下文的概念。

BeanFactory newFactory = new XmlBeanFactory(xmlResource, yourParentBeanFactory);

// if you what an ApplicationContext

ApplicationContext newContext = new ClassPathXmlApplicationContext( new String[]{"newBeans.xml"}, parent);
Run Code Online (Sandbox Code Playgroud)

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

新上下文中无法在其自身内解析的引用将在父级之间传递到其父级。

请注意,您可以通过实现来获取当前应用程序上下文ApplicationContextAware

  • 回复很晚了,是的,但提问者需要参考现有的上下文。 (2认同)