使用 Felix 容器中的 Spring DM 框架構建 Java 類並將其打包為 OSGi 包。這篇文章是本系列的第 2 部分,向您介紹如何使用 Spring 框架創建包,然後將其部署到 Felix 運行時環境。您將了解如何通過基於簡單的 Spring 配置刪除核心 OSGi 框架依賴項。
引言
在本文中,您將回顧在本系列的第 1 部分中開發的舊應用程序。該應用程序現在將使用 Spring DM 構建和打包這些包。應用程序客戶端將調用服務組件來處理訂單,並且伺服器組件將列印訂單 ID。本文將使用基於 Felix 的 OSGi 容器幫助您了解 Spring DM 的概念及其用法。
系統要求
要運行本文中的示例,請確保已在計算機上安裝和設置了以下軟體:
安裝上述分發版以後,設置以下環境變數:(通過示例集 ANT_HOME=C:\apache-ant-1.7.0)。
接下來,將以下內容添加到 PATH 環境變數:
Spring DM
Spring DM 包括幫助在 OSGi 環境中部署 Spring 應用程序的 JAR 或包。基於 Spring DM 的應用程序可以使用 OSGi 環境提供的服務。此類應用程序在開發基於 OSGi 的應用程序方面提供了便利。Spring DM 可以在 OSGi 環境中提供以下好處:
回顧訂單應用程序
我們將回顧本系列的第 1 部分中開發的訂單應用程序。正如您所了解到的,類目前與 OSGi 框架緊密地耦合在一起。現在將通過使用 Spring DM 取消這種緊密耦合狀態,將這些類作為簡單的 POJO。下面,讓我們看一下修改的 OrderClient.java。
package order.client; import order.OrderService; public class OrderClient { private OrderService orderService; public void setOrderService(OrderService orderService) { this.orderService = orderService; } public void removeService() { this.orderService = null; } public void start() { orderService.processOrder(); } public void stop() { System.out.println("Bundle stopped"); } } |
OSGi 框架依賴項已完全刪除。此類是一個普通的 POJO,僅使用 start() 方法處理訂單。沒有使用 ServiceTracker 類。
package order.impl; import order.OrderService; public class OrderServiceImpl implements OrderService { public void start() { System.out.println("Order Service registered"); } public void stop() { System.out.println("Order Service stopped"); } public void processOrder() { System.out.println("Order id: ORD123") ; } } |
類似地,上面的 OrderServiceImpl 也是一個使用 processOrder() 方法的簡單 POJO。與 OSGi 核心組件沒有關聯。而且,客戶端或服務類都不實現 BundleActivator。此處的包生命周期由 Spring DM 管理。
那麼,這些簡單的 POJO 如何作為 OSGi 組件使用?奧秘就在 Spring 配置文件中。您將在該文件中定義 OSGi 部分。
但在定義 XML 文件之前,需要對清單文件進行一些更改。該清單將包括導入包,而導入包又包含 Spring DM 文件。這可以確保 Spring 現在可以接管 OSGi 包的管理和生命周期。下面的代碼段說明了服務和客戶端清單文件。
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Order Service Bundle-SymbolicName: orderservice Bundle-Version: 1.0.0 Import-Package: org.springframework.beans.factory.xml, org.springframework.aop, org.springframework.aop.framework, org.aopalliance.aop, org.xml.sax, org.osgi.framework, org.springframework.osgi.service.importer.support, org.springframework.beans.propertyeditors, org.springframework.osgi.service.exporter.support, org.springframework.osgi.service.exporter Export-Package: order |
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Order Service Client Bundle-SymbolicName: orderclient Bundle-Version: 1.0.0 Import-Package: org.springframework.beans.factory.xml, org.springframework.aop, org.springframework.aop.framework, org.aopalliance.aop, org.xml.sax, org.osgi.framework, org.springframework.osgi.service.importer.support, org.springframework.beans.propertyeditors, org.springframework.osgi.service.importer, org.springframework.osgi.service.exporter.support, order |
您將創建兩個 XML 文件,分別用於服務組件和客戶端組件。對於該服務,orderservice.xml XML 文件將定義訂單服務實現 Bean,orderservice-osgi.xml XML 文件將定義訂單服務介面並引用其實現。類似地,對於客戶端,XML 文件將定義訂單客戶端 Bean,並引用訂單服務組件。服務和客戶端 XML 文件位於各自的 META-INF/spring 文件夾下。下面的代碼顯示了服務 XML 文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="orderService" class="order.impl.OrderServiceImpl"/> </beans> |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="orderService" class="order.impl.OrderServiceImpl"/> </beans> |
實際上,您可以在一個 XML 文件中同時提供 Bean 和 OSGi 定義。沒必要創建兩個單獨的文件。這裡我們使用了兩個單獨的文件是為了能夠區分介面定義和配置。這樣管理和維護這些文件就變得非常容易。基於 Spring 的配置的誘人之處就是它可以讓您在 OSGi 容器外測試訂單服務 Bean。
為了能夠實際做到這一點,您需要在 Felix 中安裝相關的 Spring DM jar。
felix.auto.start.1= \ file:/felix-1.0.4/bundle/org.apache.felix.shell-1.0.1.jar \ file:/felix-1.0.4/bundle/org.apache.felix.shell.tui-1.0.1.jar \ file:/felix-1.0.4/bundle/org.apache.felix.bundlerepository-1.0.3.jar \ file:/osgi_spring/order/springlib/aopalliance.osgi-1.0-SNAPSHOT.jar \ file:/osgi_spring/order/springlib/jcl104-over-slf4j-1.4.3.jar \ file:/osgi_spring/order/springlib/log4j.osgi-1.2.15-SNAPSHOT.jar \ file:/osgi_spring/order/springlib/org.apache.felix.main-1.0.1.jar \ file:/osgi_spring/order/springlib/slf4j-api-1.4.3.jar \ file:/osgi_spring/order/springlib/slf4j-log4j12-1.4.3.jar \ file:/osgi_spring/order/springlib/spring-aop-2.5.1.jar \ file:/osgi_spring/order/springlib/spring-beans-2.5.1.jar \ file:/osgi_spring/order/springlib/spring-context-2.5.1.jar \ file:/osgi_spring/order/springlib/spring-core-2.5.1.jar \ file:/osgi_spring/order/springlib/spring-osgi-core-1.0.2.jar \ file:/osgi_spring/order/springlib/spring-osgi-extender-1.0.2.jar \ file:/osgi_spring/order/springlib/spring-osgi-io-1.0.2.jar |
從上面的演示中可以看出,Felix 配置文件需要定義 Spring DM 包。您可以下載 Spring DM 包並將其放在您選擇的文件夾中。在將 jar 放在適當的文件夾中之後,使用 Felix 配置文件中的相關項指定在 Felix 啟動時安裝這些 jar。
然後可以在 Felix 運行時環境中繼續執行安裝客戶端和服務包的步驟。在安裝之後,您應能夠啟動和停止這些包並會看到相同的結果。您會注意到,與基於 Felix 的 OSGi 不同,這次是 Spring DM 管理包的生命周期。
結束語
[火星人 ] OSGi 和 Spring,第 2 部分: 使用 Felix 構建 OSGi 並將其作為 S已經有609次圍觀