java: Spring: How to transfer beans from one ClassPathXMLApplicationContext to another?

巧了我就是萌 提交于 2019-12-14 02:16:27

问题


How to transfer beans from one ClassPathXMLApplicationContext to another?

I create one context like this

ClassPathXMLApplicationContext myOneContext = new ClassPathXMLApplicationContext("path to my  xml bean definitions"); // It loads 10 beans which probably refer each other

ClassPathXMLApplicationContext my2ndContext = new ClassPathXMLApplicationContext("path to my  xml bean definitions"); // It loads 2 beans, which probably refer each other

is it possible to transfer all beans from my2ndContext to myOneContext?

I have been getting beanFactory from myOneContext and getting its internal bean factory as DefaultSingletonBeanRegistry and invoking registerSingleton

Object beanObject = my2ndContext.getBean("beanName");
DefaultSingletonBeanRegistry lbf =(DefaultSingletonBeanRegistry)myOneContext.getBeanFactory();
lbf.registerSingleton("beanName", beanObject);

is this ok? i feel i am doing a kind of hack. also not sure what else i am missing.

Another option is to retain the beans in the same contexts and add a parent relation among the AppContexts and its bean factories.

myOneContext.setParent(my2ndContext);
DefaultListableBeanFactory lbf = (DefaultListableBeanFactory)myOneContext.getBeanFactory();
lbf.setParentBeanFactory(my2ndContext.getBeanFactory());

this way all the beans from both context are visible in the myOneContext

But a problem arises when i have to destroy the my2ndContext and i set the parent of the BeanFactory to null.

DefaultListableBeanFactory lbf = (DefaultListableBeanFactory)myOneContext.getBeanFactory();
lbf.setParentBeanFactory(null); <<< throws exception

because it doesnt allow the bean factory to be changed.

From Spring Sourse: AbstractBeanFactory.java
public void setParentBeanFactory(BeanFactory parentBeanFactory) {
    if (this.parentBeanFactory != null && this.parentBeanFactory != parentBeanFactory) {
        throw new IllegalStateException("Already associated with parent BeanFactory: " + this.parentBeanFactory);
    }
    this.parentBeanFactory = parentBeanFactory;
}

which way should i choose? transfer the beans OR make a parent relation. which is preferable?

Thanks,

Regards,

Vimal


回答1:


I recommend setting up a dedicated parent context with all shared infrastructure inside. The application specific contexts would use this one as their parent. It would be possible to refresh the dynamic context while leaving shared part in parent intact.

It is best to keep parent-child hierarchy flat - one parent context and multiple child contexts. Cross-dependencies between sibling application context while tempting at first, would contribute to chaos later on and supporting such dependencies would require some non-trivial hacks or workarounds.

If this kind of sibling dependencies is a must, have a look at OSGI.



来源:https://stackoverflow.com/questions/9885350/java-spring-how-to-transfer-beans-from-one-classpathxmlapplicationcontext-to-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!