上篇文章中介绍了如何使用独立的Equinox发行包搭建OSGI运行环境,而不是依赖与具体的Eclipse基础开发工具,本文开始介绍如何使用Blueprint將Spring框架整合到OSGI中。

一、开发一个自己Bundle

在整合之前,我们接着上篇文章的内容,先来开发一个自己的Bundle。

首先新建一个Plug-in Project,名称为com.csdn.osgi.common,如下图:



an OSGI framework选项依然选择standard,表示使用标准的OSGI规范,单击Next按钮,进入如下界面:



单击Next按钮,如下图所示,选择Hello OSGI Bundle,然后单击Finish按钮。



再次打开Eclipse自带的Equinox启动工具,即可看到我们开发的Bundle,如下图所示,如果未勾选,需要手动勾选上。



单击Debug按钮启动Equinox框架,控制台中输入ss命令,如下:

Hello World!!
osgi> ss
"Framework is launched." id State Bundle
0 ACTIVE org.eclipse.osgi_3.10.0.v20140606-1445
5 ACTIVE org.apache.felix.gogo.command_0.10.0.v201209301215
6 ACTIVE org.apache.felix.gogo.runtime_0.10.0.v201209301036
7 ACTIVE org.apache.felix.gogo.shell_0.10.0.v201212101605
8 ACTIVE org.eclipse.equinox.console_1.1.0.v20140131-1639
9 ACTIVE com.csdn.osgi.common_1.0.0.qualifier
osgi>

控制台中输出了Hello World!!,说明我们自己开发的Bundle已经成功运行,接下来就开始介绍如何將Spring框架整合到OSGI中。

二、Blueprint介绍

整合Spring框架到OSGI中需要用到Blueprint,我们有必要先来了解一下Blueprint是什么。大家都知道,现在几乎任何一个Java EE项目都离不开Spring框架,在Blueprint诞生之前,SpringSource组织为了推动OSGI的发展,为Spring和OSGI的整合提供一套解决方案,这套解决方案就是Spring Dynamic Modules,简称Spring DM。

OSGi Alliance组织在OSGi 4.2中基于Spring Dynamic Modules 引入了Blueprint服务规范,也就是说Blueprint规范源于Spring DM。目前我们依然可以使用Spring DM將Spring框架整合到OSGI中,但是SpringSource已经放弃了Spring DM,该项目已经不在更新,据说是因为Spring框架的核心思想是创建全局共享的bean容器,而OSGI的思想是实现模块化,每个Bundle使用不同的类加载器加载,Bundle之间通过服务注册实现通信,Spring这种“共享”的理念与OSGI模块化思想本身就存在冲突。笔者也发现不少抨击Spring DM的文章,其实没有必要,没有任何解决方案是十全十美的,不然为什么一直会有新技术诞生呢。

言归正传,Blueprint已正式成为OSGI规范,具体的实现有Apache Aries和Gemini Blueprint,Gemini Blueprint的前生就是Spring DM,SpringSource將Spring DM捐献给了Eclipse组织,也就有了目前的Gemini Blueprint。

由于Blueprint是OSGI规范,而且Spring DM现在已经不在更新,所以笔者打算使用Gemini Blueprint实现Spring框架与OSGI的整合。

三、整合Spring框架

1、获取Spring Bundle

要整合Spring框架,我们首先需要获取Spring Bundle,由于Gemini Blueprint仅支持Spring 3.0以上版本,我们从Spring官网中下载的Spring Release包中的jar包都是普通的Jar包,而不是一个完整的Bundle,所以我们不得不从其他渠道获取Bundle版本的Spring Jar包。这点我们不得不感谢SpringSource团队了,SpringSource提供了一个SpringSource Enterprise Bundle Repository网站,地址如下:

http://ebr.springsource.com/repository/app/bundle

我们可以从这个网站中检索所有需要的Bundle版本的Jar包,如下图所示:



一些开源框架(例如ibatis)、数据库驱动等的Bundle版Jar包都可以从该网站下载到,例如我们需要获取Spring框架的Bundle时,只需要在右侧搜索框输入spring,然后按下Enter键即可,搜索结果如下:



笔者选择Spring Framework 3.0.0版本进行演示,单击链接进入如下界面:



读者可以单击下方链接,逐一下载每一个Bundle,但是这样做比较繁琐,这里介绍一个小技巧,我们可以使用Maven获取,在任意目录新建一个pox.xml文件,内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>test1</groupId>
<artifactId>test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>test1</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.aop</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.transaction</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.servlet</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context.support</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.jdbc</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
</dependencies>
</project>

然后执行如下命令:

mvn dependency:copy-dependencies -DoutputDirectory=libs  -DincludeScope=runtime

命令执行完毕后,会将所有Bundle下载到pom.xml同级目录的libs子目录下,生成的Bundle如下:

    com.springsource.org.aopalliance-1.0.0.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.jdbc-3.0.0.RELEASE.jar
org.springframework.transaction-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar

接着需要在DynamicRuntime工程中,新建一个spring目录,然后將Spring的Bundle放到spring目录下,如下图所示:

2、获取Gemini Blueprint

Gemini Blueprint的获取相对简单,读者可以从Eclipse官网下载,笔者选择的版本为gemini-blueprint-2.0.0.M02,下载地址如下:

http://www.eclipse.org/downloads/download.php?file=/blueprint/gemini-blueprint-2.0.0.M02.zip

为保证下载速度,读者可以选择一个国内的镜像,下载完毕后,解压目录结构如下:



dist目录下为Gemini Blueprint所有Bundle,docs目录下为帮助文档,dist目录下内容如下:

    gemini-blueprint-core-2.0.0.M02-javadoc.jar
gemini-blueprint-core-2.0.0.M02-sources.jar
gemini-blueprint-core-2.0.0.M02.jar
gemini-blueprint-extender-2.0.0.M02-javadoc.jar
gemini-blueprint-extender-2.0.0.M02-sources.jar
gemini-blueprint-extender-2.0.0.M02.jar
gemini-blueprint-io-2.0.0.M02-javadoc.jar
gemini-blueprint-io-2.0.0.M02-sources.jar
gemini-blueprint-io-2.0.0.M02.jar
gemini-blueprint-mock-2.0.0.M02-javadoc.jar
gemini-blueprint-mock-2.0.0.M02-sources.jar
gemini-blueprint-mock-2.0.0.M02.jar
gemini-blueprint-test-2.0.0.M02-javadoc.jar
gemini-blueprint-test-2.0.0.M02-sources.jar
gemini-blueprint-test-2.0.0.M02.jar

我们需要在DynamicRuntime工程中,新建一个blueprint目录,然后將下面几个Bundle复制到该目录下:

    gemini-blueprint-core-2.0.0.M02.jar
gemini-blueprint-extender-2.0.0.M02.jar
gemini-blueprint-io-2.0.0.M02.jar

完成后项目结构如下图所示:



到目前为止我们已经完成了整合Spring框架的准备工作,本文的内容已经比较多了,休息一下,下篇文件继续介绍使用Blueprint整合Spring框架。

OSGI企业应用开发(四)使用Blueprint整合Spring框架(一)的更多相关文章

  1. OSGI企业应用开发(八)整合Spring和Mybatis框架(一)

    到目前为止,我们已经学习了如何使用Blueprint將Spring框架整合到OSGI应用中,并学习了Blueprint&Gemini Blueprint的一些使用细节.本篇文章开始,我们將My ...

  2. OSGI企业应用开发(十)整合Spring和Mybatis框架(三)

    上篇文章中,我们已经完成了OSGI应用中Spring和Mybatis框架的整合,本文就来介绍一下,如何在其他Bundle中,使用Mybatis框架来操作数据库. 为了方便演示,我们新建一个新的Plug ...

  3. OSGI企业应用开发(九)整合Spring和Mybatis框架(二)

    上篇文章中,我们完成了在OSGI应用中整合Spring和Mybatis框架的准备工作,本节我们继续Spring和Mybatis框架的整合. 一.解决OSGI整合Spring中的Placeholder问 ...

  4. OSGI企业应用开发(五)使用Blueprint整合Spring框架(二)

    上篇文章中,我们开发了一个自定义的Bundle,接着从网络中下载到Spring和Blueprint的Bundle,然后复制到DynamicRuntime项目下. 需要注意的是,这些Bundle并不能在 ...

  5. OSGI企业应用开发(七)细说Blueprint & Gemini Blueprint(二)

    上篇文章介绍了标准的Blueprint 规范与 Gemini Blueprint如何自定义Bean配置文件路径,本文接着上篇文章继续介绍Blueprint的使用. 一.Bean的配置 前面提到过,Ge ...

  6. OSGI企业应用开发(六)细说Blueprint & Gemini Blueprint(一)

    上篇文章介绍了如何使用Blueprint將Spring框架整合到OSGI应用的Bundle中,从上篇文章中我们大概了解了Blueprint与Gemini Blueprint的关系,简单的说,Bluep ...

  7. OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境

    上篇文章介绍了如何在Eclipse中搭建Felix的运行环境,我们需要將Bundle发布到Felix框架的bundle目录下,Felix框架启动时才会自动加载这些Bundle,否则需要在Felix框架 ...

  8. 整合Spring框架和MyBatis框架

    ------------------------siwuxie095                                 整合 Spring 框架和 MyBatis 框架         ...

  9. 整合Spring框架和Hibernate框架

    -------------------siwuxie095                                 整合 Spring 框架和 Hibernate 框架         1.导 ...

随机推荐

  1. Python学习之二

    基础语法 一.起始行 #!/usr/bin/python 或 #!/usr/bin/env python 目的是在运行python脚本的时候告诉系统我们要用Python解释器去运行py脚本 # -*- ...

  2. linq查询时查询语句中附带多个查询时“已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭”

    主要原因是因为EF采用的 DataReader来进行数据的存储,此时connection使用的是同一个. 例如: list = _tzNewsService.GetAll().Where(w => ...

  3. KMP算法的next函数求解和分析过程

    转自 wang0606120221:http://blog.csdn.net/wang0606120221/article/details/7402688 假设KMP算法中的模式串为P,主串为S,那么 ...

  4. SQL Server性能优化(7)理解数据库文件组织

    一.基本单位"页"     SQL Server是用8KB的页来存储数据.物理I/O操作也是在页级执行.页的种类有很多,具体参考(MSDN).我们关注更多的是数据页的结构,包括三部 ...

  5. Nginx 配置 Location 规则优先级问题

    nginx 的 location与配置中 location 顺序没有关系,与 location 表达式的类型有关.相同类型的表达式,字符串长的会优先匹配. 以下是按优先级排列说明: 等号类型(=)的优 ...

  6. Android 开发工具类 35_PatchUtils

    增量更新工具类[https://github.com/cundong/SmartAppUpdates] import java.io.File; import android.app.Activity ...

  7. quartz ? * 区别

    官方文档上提到问号时是这样说的: The '?' character is allowed for the day-of-month and day-of-week fields. It is use ...

  8. 【IT笔试面试题整理】给定一个数组a[N]构造数组b [N]

    [来源]:腾讯2013实习生笔试   给定一个数组a[N],我们希望构造数组b [N],其中b[j]=a[0]*a[1]-a[N-1] / a[j])空间复杂度和O(n)的时间复杂度:除遍历计数器与a ...

  9. CRM项目测试第一天

    经过前几天代码的修改,界面的完善.主要的功能都实现了!今天主要是交换各组的项目,互相来测试,找bug. 在互相测试的过程,我听见有一组应该算是讨论的比价激烈的!我们组我们自己找到了bug,但是测试我们 ...

  10. Java创建线程的两种方式

    方式 继承Thread类 实现Runnable方法 实例 #继承Thread类 public class ThreadTest2 extends Thread { private int thread ...