OSGi與Spring:設(shè)置Spring DM開發(fā)環(huán)境
譯文【51CTO精選譯文】本文是《你好,OSGi》系列的第六部分。之前我們介紹了什么是Spring動(dòng)態(tài)模型(Spring DM),下面我們將學(xué)習(xí)如何設(shè)置Spring DM開發(fā)環(huán)境,并準(zhǔn)備使用Spring DM開發(fā)您的***個(gè)Hello World應(yīng)用。
51CTO編輯推薦:OSGi入門與實(shí)踐全攻略
設(shè)置Spring DM開發(fā)環(huán)境
Spring DM框架 1.0(Spring Dynamic Modules framework 1.0 )支持與 OSGi Specification Release 4(或更高版本)以及 JDK 1.4(或更高版本)兼容的 OSGi 套件的開發(fā)。Spring DM還針對三個(gè)開源 OSGi 容器實(shí)施(Equinox 3.2.2、Felix 1.0.1 和 Knopflerfish 2.0.3)進(jìn)行了測試,所以你可以在這三個(gè)容器實(shí)施中執(zhí)行你的代碼。
在 本節(jié)的開發(fā)示例中,我使用了 Eclipse 3.3,它嵌入了 Equinox 3.3 OSGi 容器。Eclipse IDE 為 OSGi 套件的開發(fā)、執(zhí)行和調(diào)試提供很好的支持。我使用 JDK 1.5 測試了本節(jié)中的示例代碼。請按照以下步驟使用 Eclipse IDE 設(shè)置開發(fā)環(huán)境:
1. 下載 Spring DM。寫作本文時(shí),其發(fā)布版本為 1.0.2。請確保下載 spring-osgi-1.0.2-with-dependencies.zip 或***的zip文件。
2. 解壓縮 spring-osgi-1.0.2-with-dependencies.zip ,將其內(nèi)容保存在你的計(jì)算機(jī)上,比如可保持到 c:\software 文件夾中。
3. 啟動(dòng) Eclipse IDE。但需要選擇工作空間(workspace),請選擇空文件夾,比如 c:\sample\springdm。
4. 導(dǎo)入該項(xiàng)目所需的所有 Spring DM JAR 文件。在 Package Explorer 上右擊,然后點(diǎn)擊 Import --> Plug-in Development --> Plug-ins and Fragments,然后單擊下一步,將彈出 Import Plug-ins and Fragments 對話框。
5. 在該對話框中,取消選擇目標(biāo)平臺復(fù)選框(在 Preferences 中指定)。
6. Plug-in Location 的瀏覽(Browse)按鈕現(xiàn)在應(yīng)該是激活狀態(tài);單擊它,然后選擇 C:\software\spring-osgi-1.0.2\dist,如圖1 所示。
圖1.導(dǎo)入 Spring DM JAR 文件
7. 單擊下一步,彈出 Selection 對話框;將以下三個(gè)插件添加到你的“Plug-ins and Fragments to import”中:
- org.springframeork.osgi.bundle.core
- org.springframeork.osgi.bundle.extender
- org.springframeork.osgi.bundle.io
現(xiàn)在單擊完成。Eclipse 會(huì)將這三個(gè)套件導(dǎo)入到你的工作空間中,在那里你應(yīng)能夠在 Package Explorer 視圖中看到它們。
8. 下一步,你要從 C:\software\spring-osgi-1.0.2\lib 再導(dǎo)入幾個(gè)套件,所以請返回并重復(fù)步驟 4 和步驟 5。在步驟 6 中,選擇插件位置(plug-in location)為:C:\software\spring-osgi-1.0.2\lib,然后單擊下一步。
9. 在 Selection 對話框中,你將會(huì)看到更多能夠使用的插件,請選擇以下插件:
- org.springframeork.bundle.spring.aop
- org.springframeork.bundle.spring.beans
- org.springframeork.bundle.spring.context
- org.springframeork.bundle.spring.core
- org.springframeork.bundle.spring.jdbc
- org.springframeork.bundle.spring.tx
- org.springframeork.osgi.aopalliance.osgi
- backport-util-concurrent.osgi-3.0-SNAPSHOT.jar (if you're using JDK 1.4)
選擇完成之后,請單擊完成,Eclipse 會(huì)將這七個(gè)套件導(dǎo)入到你的工作空間中。
Apache Commons Logging 是 Spring DM的日志框架。Spring DM提供了OSGi 套件格式的 Apache Commons Logging 以及 Apache Log4j JAR。不過,這些套件好像有些問題,因?yàn)槲沂褂?Eclipse 無法將他們導(dǎo)入。如果你遇到同樣的問題,請按照以下步驟,創(chuàng)建你自己的 OSGi 套件,其中包含了這兩個(gè) JAR:
1. 在 Eclipse 中,單擊 File --> New --> Project -> Plug-in Development --> Plug-in from existing JAR archives,然后點(diǎn)擊下一步。Eclipse 將打開 JAR Selection 對話框。
2. 在該對話框中,單擊 Add External 按鈕,彈出 File Selection 對話框。此處,你可以選擇 commons-logging.jar 和 log4j-1.2.14.jar。你的 JAR Selection 對話框有與圖2中的截圖類似。
圖2. 選擇插件中要包含的 JAR
單擊下一步,Eclipse 將彈出 Plug-in Project Properties 對話框。
3. 在 Plug-in Project Properties 對話框中,請輸入項(xiàng)目名稱——與 org.apache.commons.logging 類似。在 Target Platform 部分,請選擇 OSGi Framework --> Standard,然后單擊完成。
設(shè)置Spring DM開發(fā)環(huán)境的步驟就是這樣,開發(fā) Spring DM插件所需的 Eclipse 環(huán)境已經(jīng)準(zhǔn)備完畢。在下面一篇文章中,我們將使用 Spring DM來開發(fā)一個(gè) Hello World 應(yīng)用程序。
【編輯推薦】