簡體   English   中英

訪問OSGi包中的Spring上下文

[英]access Spring context in OSGi bundle

我有一個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 Dynamic Modules加載你的上下文 - 所以你不應該為自己做這個。 看看Spring DM 文檔 - 這將非常有用。

Spring DM將應用程序上下文發布為OSGi服務。 請查看此處的說明並遵循建議。

另見下面的評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM