zoukankan      html  css  js  c++  java
  • 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框架。

  • 相关阅读:
    Python 学习日记 第七天
    Python 学习日记 第六天
    Python 学习日记 第五天
    Python 学习日记 第四天
    Redis 中的数据类型及基本操作
    Asp.net mvc 中View 的呈现(二)
    Asp.net mvc 中View的呈现(一)
    Asp.net mvc 中Action 方法的执行(三)
    Asp.net mvc 中Action 方法的执行(二)
    Asp.net mvc 中Action 方法的执行(一)
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6468183.html
Copyright © 2011-2022 走看看