2010-09-24 96 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