【问题标题】:access Spring context in OSGi bundle在 OSGi 包中访问 Spring 上下文
【发布时间】:2011-04-16 21:28:53
【问题描述】:

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

<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 句柄的正确方法是什么?

【问题讨论】:

    标签: spring osgi apache-servicemix fuse


    【解决方案1】:

    我想是 Spring Dynamic Modules 加载了你的上下文 - 所以你不应该为自己做这件事。看看 Spring DM documentation - 这会很有用。

    Spring DM 会将应用程序上下文发布为 OSGi 服务。看看here 的解释并遵循建议。

    另请参阅下面的评论。

    【讨论】:

    • 谢谢,我已阅读此文档...我仍然看不到一个简单的方法来做到这一点。也许跳过一些 OSGI 注册表箍来获取上下文句柄,但这应该是 OSGi 中的 Spring 的标准操作。我错过了什么吗?我只想要一个在 Spring 中定义的 bean 实例?
    • 我刚刚发现有一个Spring-Core ApplicationContextAware Interface可以实现。 Context 将被注入到您的实现类中。这应该可行,并且是一个更简单的解决方案。您可以在网上找到示例,例如howto-notes.blogspot.com/2009/03/…
    猜你喜欢
    • 2011-10-08
    • 2014-07-24
    • 2011-05-07
    • 2012-07-22
    • 1970-01-01
    • 2011-09-22
    • 2014-11-04
    • 2012-04-28
    • 1970-01-01
    相关资源
    最近更新 更多