OSGI企业应用开发(四)使用Blueprint整合Spring框架(一)
上篇文章中介绍了如何使用独立的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框架(一)的更多相关文章
- OSGI企业应用开发(八)整合Spring和Mybatis框架(一)
到目前为止,我们已经学习了如何使用Blueprint將Spring框架整合到OSGI应用中,并学习了Blueprint&Gemini Blueprint的一些使用细节.本篇文章开始,我们將My ...
- OSGI企业应用开发(十)整合Spring和Mybatis框架(三)
上篇文章中,我们已经完成了OSGI应用中Spring和Mybatis框架的整合,本文就来介绍一下,如何在其他Bundle中,使用Mybatis框架来操作数据库. 为了方便演示,我们新建一个新的Plug ...
- OSGI企业应用开发(九)整合Spring和Mybatis框架(二)
上篇文章中,我们完成了在OSGI应用中整合Spring和Mybatis框架的准备工作,本节我们继续Spring和Mybatis框架的整合. 一.解决OSGI整合Spring中的Placeholder问 ...
- OSGI企业应用开发(五)使用Blueprint整合Spring框架(二)
上篇文章中,我们开发了一个自定义的Bundle,接着从网络中下载到Spring和Blueprint的Bundle,然后复制到DynamicRuntime项目下. 需要注意的是,这些Bundle并不能在 ...
- OSGI企业应用开发(七)细说Blueprint & Gemini Blueprint(二)
上篇文章介绍了标准的Blueprint 规范与 Gemini Blueprint如何自定义Bean配置文件路径,本文接着上篇文章继续介绍Blueprint的使用. 一.Bean的配置 前面提到过,Ge ...
- OSGI企业应用开发(六)细说Blueprint & Gemini Blueprint(一)
上篇文章介绍了如何使用Blueprint將Spring框架整合到OSGI应用的Bundle中,从上篇文章中我们大概了解了Blueprint与Gemini Blueprint的关系,简单的说,Bluep ...
- OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境
上篇文章介绍了如何在Eclipse中搭建Felix的运行环境,我们需要將Bundle发布到Felix框架的bundle目录下,Felix框架启动时才会自动加载这些Bundle,否则需要在Felix框架 ...
- 整合Spring框架和MyBatis框架
------------------------siwuxie095 整合 Spring 框架和 MyBatis 框架 ...
- 整合Spring框架和Hibernate框架
-------------------siwuxie095 整合 Spring 框架和 Hibernate 框架 1.导 ...
随机推荐
- git commit --amend的撤销方法
某同事执行git commit 时太兴奋,执行了 git commit --amend 慌了,不敢编辑上一个commit的description了,直接选择了wq退出,然而git毕竟强大,默认将改动合 ...
- CompoundScope说明
A class scope adds capabilities to keep track of changes in relatedclass scopes - this allows client ...
- Python -- 网络编程 -- 简单抓取网页
抓取网页: urllib.request.urlopen(url).read().decode('utf-8') --- (百度是utf-8,谷歌不是utf-8,也不是cp936,ascii也不行 ...
- ASP.NET 数据绑定到列表控件
<div> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> ...
- 【杂谈】对CopyOnWriteArrayList的认识
前言 之前看<Java并发编程>这本书的时候,有看到这个,只记得"读多写少"."写入时复制".书中没有过多讲述,只是一笔带过(不过现在回头看,发现讲 ...
- 制作openstack使用的Ubuntu镜像
一.环境准备 OS:Ubuntu-14.04 制作镜像版本:Ubuntu-14.04.4-server-amd64.iso 查看是否支持虚拟化(有输出代表支持,否则在BIOS页面中设置即可): egr ...
- 大数据之presto
1.概述 Presto是一个分布式SQL查询引擎,用于查询分布在一个或多个不同数据源中的大数据集.presto可以通过使用分布式查询,可以快速高效的完成海量数据的查询.它是完全基于内存的,所以速度非常 ...
- git 分支 branch 操作
创建分支 git branch test: 基于当前commit创建test分支..git/HEAD 文件中记录了当前分支名字. 删除分支 git branch -d test:删除本地test分支 ...
- AJAX unsupported media type 415错误处理
一.问题 在使用angular做请求拦截时,因为依赖循环的问题,在请求拦截中改为使用ajax来发起请求拿到我想要的数据,结果出现了415 Unsupported Media Type错误,由于很久没使 ...
- C#Redis哈希Hashes
一.前戏 我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和Age ...