OSGi与Spring:设置Spring DM开发环境

译文
开发 后端
Spring DM框架支持OSGi套件的开发,因此是学习OSGi的一个绝佳环境。本文介绍如何设置Spring DM开发环境,文中使用的开发工具为Eclipse 3.3。

【51CTO精选译文】本文是《你好,OSGi》系列的第六部分。之前我们介绍了什么是Spring动态模型(Spring DM),下面我们将学习如何设置Spring DM开发环境,并准备使用Spring DM开发您的***个Hello World应用。

51CTO编辑推荐:OSGi入门与实践全攻略

设置Spring DM开发环境

Spring DM框架 1.0(Spring Dynamic Modules framework 1.0 )支持与 OSGi Specification Release 4(或更高版本)以及 JDK 1.4(或更高版本)兼容的 OSGi 套件的开发。Spring DM还针对三个开源 OSGi 容器实施(Equinox 3.2.2、Felix 1.0.1 和 Knopflerfish 2.0.3)进行了测试,所以你可以在这三个容器实施中执行你的代码。

在 本节的开发示例中,我使用了 Eclipse 3.3,它嵌入了 Equinox 3.3 OSGi 容器。Eclipse IDE 为 OSGi 套件的开发、执行和调试提供很好的支持。我使用 JDK 1.5 测试了本节中的示例代码。请按照以下步骤使用 Eclipse IDE 设置开发环境:

1. 下载 Spring DM。写作本文时,其发布版本为 1.0.2。请确保下载 spring-osgi-1.0.2-with-dependencies.zip 或***的zip文件。

2. 解压缩 spring-osgi-1.0.2-with-dependencies.zip ,将其内容保存在你的计算机上,比如可保持到 c:\software 文件夹中。

3. 启动 Eclipse IDE。但需要选择工作空间(workspace),请选择空文件夹,比如 c:\sample\springdm。

4. 导入该项目所需的所有 Spring DM JAR 文件。在 Package Explorer 上右击,然后点击 Import --> Plug-in Development --> Plug-ins and Fragments,然后单击下一步,将弹出 Import Plug-ins and Fragments 对话框。

5. 在该对话框中,取消选择目标平台复选框(在 Preferences 中指定)。

6. Plug-in Location 的浏览(Browse)按钮现在应该是激活状态;单击它,然后选择 C:\software\spring-osgi-1.0.2\dist,如图1 所示。

导入 Spring DM JAR 文件 

图1.导入 Spring DM JAR 文件

7. 单击下一步,弹出 Selection 对话框;将以下三个插件添加到你的“Plug-ins and Fragments to import”中:

  1. org.springframeork.osgi.bundle.core  
  2. org.springframeork.osgi.bundle.extender  
  3. org.springframeork.osgi.bundle.io 

现在单击完成。Eclipse 会将这三个套件导入到你的工作空间中,在那里你应能够在 Package Explorer 视图中看到它们。

8. 下一步,你要从 C:\software\spring-osgi-1.0.2\lib 再导入几个套件,所以请返回并重复步骤 4 和步骤 5。在步骤 6 中,选择插件位置(plug-in location)为:C:\software\spring-osgi-1.0.2\lib,然后单击下一步。

9. 在 Selection 对话框中,你将会看到更多能够使用的插件,请选择以下插件:

  1. org.springframeork.bundle.spring.aop  
  2. org.springframeork.bundle.spring.beans  
  3. org.springframeork.bundle.spring.context  
  4. org.springframeork.bundle.spring.core  
  5. org.springframeork.bundle.spring.jdbc  
  6. org.springframeork.bundle.spring.tx  
  7. org.springframeork.osgi.aopalliance.osgi  
  8. backport-util-concurrent.osgi-3.0-SNAPSHOT.jar (if you're using JDK 1.4

选择完成之后,请单击完成,Eclipse 会将这七个套件导入到你的工作空间中。

Apache Commons Logging 是 Spring DM的日志框架。Spring DM提供了OSGi 套件格式的 Apache Commons Logging 以及 Apache Log4j JAR。不过,这些套件好像有些问题,因为我使用 Eclipse 无法将他们导入。如果你遇到同样的问题,请按照以下步骤,创建你自己的 OSGi 套件,其中包含了这两个 JAR:

1. 在 Eclipse 中,单击 File --> New --> Project -> Plug-in Development --> Plug-in from existing JAR archives,然后点击下一步。Eclipse 将打开 JAR Selection 对话框。

2. 在该对话框中,单击 Add External 按钮,弹出 File Selection 对话框。此处,你可以选择 commons-logging.jar 和 log4j-1.2.14.jar。你的 JAR Selection 对话框有与图2中的截图类似。

选择插件中要包含的 JAR 

图2. 选择插件中要包含的 JAR

单击下一步,Eclipse 将弹出 Plug-in Project Properties 对话框。

3. 在 Plug-in Project Properties 对话框中,请输入项目名称——与 org.apache.commons.logging 类似。在 Target Platform 部分,请选择 OSGi Framework --> Standard,然后单击完成。

设置Spring DM开发环境的步骤就是这样,开发 Spring DM插件所需的 Eclipse 环境已经准备完毕。在下面一篇文章中,我们将使用 Spring DM来开发一个 Hello World 应用程序。

【编辑推荐】

  1. OSGi和Spring入门:什么是Spring动态模型(Spring DM)?
  2. OSGi服务:非常适合SOA的架构
  3. OSGi依赖性管理:Bundle访问域
  4. OSGi Bundle之Hello World
  5. OSGi是什么:Java语言的动态模块系统
责任编辑:yangsai 来源: 51CTO.com
相关推荐

2009-10-09 13:42:56

Spring DataSpring DM

2009-09-27 09:29:56

OSGi和SpringSpring动态模型Spring DM

2009-09-28 14:49:08

Spring DMOSGi服务

2009-06-01 10:28:03

SpringOSGi整合

2009-03-03 10:06:00

IBMJavaOSGi

2009-06-18 15:24:08

Spring OSGi

2009-10-15 15:12:39

Equinox服务器端Equinox

2009-07-09 09:18:50

Spring Tool

2010-04-07 08:55:00

OSGiSpring

2009-06-18 15:14:53

Spring osgi

2009-06-23 17:54:41

OSGi与JSF

2009-10-29 09:52:55

OSGi

2009-06-01 10:40:26

SpringOSGI中文手册

2009-08-07 09:47:12

SpringSourc

2021-11-16 11:45:00

SpringSpring ClouJava

2023-09-04 11:52:53

SpringMVC性能

2010-05-28 09:22:06

dm ServerSpringEclipse

2023-07-10 08:43:53

SpringIDEA

2019-02-21 10:38:10

Web 开发代码

2011-03-28 10:42:00

Spring
点赞
收藏

51CTO技术栈公众号