访问OSGi包中的Spring上下文

Ben*_*Day 5 spring osgi fuse apache-servicemix

我有一个OSGi包,它使用bundle-context.xml文件来初始化bean.

<bean id="myBean" class="test.MyClass">
    <property name="output" value="test"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

我有一个需要获取bean实例的工厂类.在非OSGI世界中,我总是只需要以下内容来初始化上下文并获取bean的句柄...

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bundle-context.xml");
MyClass bean = (MyClass) applicationContext.getBean("myBean");
Run Code Online (Sandbox Code Playgroud)

但是,在OSGI(FuseESB 4.2,Servicemix4)中,容器会自动加载bundle-context.xml文件并初始化spring上下文.如果我显式加载上下文(使用上面的代码),则会创建2个上下文(这是不好的).那么,获取相同上下文/ bean的句柄的正确方法是什么?

FrV*_*aBe 4

我想是 Spring Dynamic Modules 加载了您的上下文 - 所以您不应该为自己执行此操作。查看 Spring DM文档- 这会很有用。

Spring DM 会将应用程序上下文发布为 OSGi 服务。请查看此处的说明并遵循建议。

另请参阅下面我的评论。