2010-09-24 85 views
5

我有一个使用bundle-context.xml文件初始化bean的OSGi包。访问OSGi包中的Spring上下文

<bean id="myBean" class="test.MyClass"> 
    <property name="output" value="test"/> 
</bean> 

我有一个需要获取bean实例的工厂类。在非OSGI的世界里,我一直只是下面的初始化上下文并得到一个处理一个bean ...

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bundle-context.xml"); 
MyClass bean = (MyClass) applicationContext.getBean("myBean"); 

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

回答

4

我想这是加载你的上下文的Spring动态模块 - 所以你不应该为你自己做。看看春天DM documentation - 这将是有用的。

Spring DM将发布应用程序上下文作为OSGi服务。请看解释here并按照建议进行操作。

另请参阅我的评论如下。

+0

thx,我读过这个文档...我还没有看到一个简单的方法来做到这一点。也许可以通过一些OSGI注册表循环来获得对上下文的处理,但这应该是OSGi中与Spring相关的标准事情。我错过了什么吗?我只想要一个在Spring中定义的bean实例? – 2010-09-27 19:49:46

+0

我刚发现有一个可以实现的Spring-Core ApplicationContextAware接口。 Context将被注入到你的实现类中。这应该工作,并成为一个更简单的解决方案。您可以在网上找到例子,例如http://howto-notes.blogspot.com/2009/03/spring-applicationcontextaware.html – FrVaBe 2010-09-28 07:12:45