---
title: "Spring与OSGi的整合（一）"
author: "benniaolk"
date: "2009-12-12T20:11:28Z"
tags: []
categories: ["企业架构"]
slug: "spring-osgi"
draft: false
lastmod: "2010-03-23T21:44:47Z"
id: 547172
url: "https://dreava.iteye.com/blog/547172"
---

1. 开发环境的准备

现在的eclipse都已经包含了Equinox，无需单独下载。  
下载最新版的Spring DM，Spring官方网站：[www.SpringSource.org](http://www.SpringSource.org)  
2. 开发OSGi的HelloWorld应用程序

在这一节，我们将开发一个OSGi bundle，演示如何利用Equinox进行OSGi bundle的开发、运行及调试，为之后的示例做准备。  
首先，新建一个Plug-in工程，如下图所示：  
 ![](http://dl.iteye.com/upload/attachment/179559/84600441-5ab6-3ea1-8f77-8da0e4d27525.bmp)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

下一步，注意选择目标平台，默认为Eclipse version \*，将其改成Equinox，如下图所示：

![](http://dl.iteye.com/upload/attachment/179561/1ea6015f-d9db-3eca-92f7-c70cf605e600.bmp)

之后按默认下一步即可，到最后一步时，无需根据模板创建工程，去掉默认的勾，如下图：

![](http://dl.iteye.com/upload/attachment/179563/28c115f9-cf2b-3665-a6c0-90d3e425375d.bmp)

点击完成，创建的工程目录如下图所示：

 ![](http://dl.iteye.com/upload/attachment/179565/a1eb8df1-6987-3b94-8676-1922914f49b1.bmp)

从图中，可以看到，eclipse为工程自动生成了一个Activator类，如下所示：

```
package helloworld;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		System.out.println("Hello World!");
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
	}

}
```

 有过eclipse插件开发经验的人应该很快就能明白，这就是该工程——实际上就是上一篇文章中所称的bundle在Equinox平台中的启动入口，相当于我们的熟悉的main函数，如果将该bundle运行到Equinox中，首先进入该bundle的是Activator的start方法，你可以将相关的服务、资源在该方法内完成向bundleContext的注册——文章后面会具体讲到相关内容。在运行期，如果想要该bundle停止运行，Equinox平台将调用Activator的stop方法，你可以在该方法内完成资源的注销等工作。  
下面我们将要完成首次的Equinox运行，将向大家展现bundle到底是如何运行起来的。为helloworld选择运行方式，如下图所示：

![](http://dl.iteye.com/upload/attachment/179567/d9de0c0c-38dc-3897-9d0e-5baf65f8dec0.bmp)

弹出如下对话框： 

![](http://dl.iteye.com/upload/attachment/179570/c234f8b9-751e-3efa-9758-4d7a5d98f2bc.bmp)

 

在弹出的对话框中，新建一个OSGi Framework运行环境（双击OSGi Framework即可，这里为其取名Equinox），选中helloworld(1.0.0)，然后点击右边的Add Required Bundles按钮，eclipse将自动选中运行helloworld的依赖bundle，如果有必要，可以点击右下的Validate Bundles验证按钮，验证程序正常运行所需的bundle是否都被选中，最后点击运行，回到控制台：  
osgi&gt; Hello World!  
可以看到，之前在Activator的start方法中的输出语句已经被输出到控制台，我们可以通过命令ss查看Equinox的运行情况，可以看到一共运行了两个bundle，如下：  
ss

Framework is launched.

id State       Bundle  
0 ACTIVE      org.eclipse.osgi\_3.4.3.R34x\_v20081215-1030  
1 ACTIVE      helloworld\_1.0.0  
还记得之前讲到过的bundle的几种状态吧？helloworld已经运行起来了。  
那么Equinox具体支持哪些命令呢？下表列出了主要的一些命令，如需查看更详细的命令清单，则可以在控制台键入help。

**类别**

**命令**

**含义**

**控制框架**

`launch`

启动框架

`shutdown`

停止框架

`close`

关闭、退出框架

`exit`

立即退出，相当于 System.exit

`init`

卸载所有 bundle（前提是已经 shutdown）

`setprop`

设置属性，在运行时进行

**控制 bundle**

`install`

安装

`uninstall`

卸载

`start`

启动

`stop`

停止

`refresh`

刷新

`update`

更新

**展示状态**

`status`

展示安装的 bundle 和注册的服务

`ss`

展示所有 bundle 的简单状态

s`ervices`

展示注册服务的详细信息

`packages`

展示导入、导出包的状态

`bundles`

展示所有已经安装的 bundles 的状态

`headers`

展示 bundles 的头信息，即 MANIFEST.MF 中的内容

`log`

展示 LOG 入口信息

**其它**

`exec`

在另外一个进程中执行一个命令（阻塞状态）

`fork`

和 EXEC 不同的是不会引起阻塞

`gc`

促使垃圾回收

`getprop`

得到属性，或者某个属性

**控制启动级别**

`sl`

得到某个 bundle 或者整个框架的 start level 信息

`setfwsl`

设置框架的 start level

`setbsl`

设置 bundle 的 start level

`setibsl`

设置初始化 bundle 的 start level

 

至此，我们已经成功的演示了helloworld，初步了解了OSGi的bundle是如何开发并运行的，下面将进入我们的正题，下面我们将通过一个稍微复杂的示例，讲解bundle之间如何进行包依赖、注册及调用服务。

 

未完待续。。

接下来的内容请看：[Spring与OSGi的整合（二）](/blog/547183)