java:春季:如何将bean从一个ClassPathXMLApplicationContext转移到另一个? [英] java: Spring: How to transfer beans from one ClassPathXMLApplicationContext to another?

查看:111
本文介绍了java:春季:如何将bean从一个ClassPathXMLApplicationContext转移到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将豆从一个ClassPathXMLApplicationContext转移到另一个?

How to transfer beans from one ClassPathXMLApplicationContext to another?

我这样创建一个上下文

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

是否可以将所有bean从my2ndContext转移到myOneContext?

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

我一直从myOneContext获取beanFactory,并将其内部的bean工厂获取为DefaultSingletonBeanRegistry并调用registerSingleton

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.

另一种选择是将bean保留在相同的上下文中,并在AppContexts及其bean工厂之间添加父关系.

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());

这样,来自两个上下文的所有bean都在myOneContext

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

但是当我必须销毁my2ndContext并且将BeanFactory的父级设置为null时,会出现问题.

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

因为它不允许更改bean工厂.

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;
}

我应该选择哪种方式?转移bean或建立父级关系.哪个更好?

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

谢谢

此致

重要

推荐答案

我建议设置一个专用的父上下文,并在其中包含所有共享的基础结构.特定于应用程序的上下文将使用此上下文作为其父级.可能会刷新动态上下文,同时保留父部分中的共享部分.

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.

如果必须具有这种同级依赖关系,请查看OSGI.

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

这篇关于java:春季:如何将bean从一个ClassPathXMLApplicationContext转移到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆