Gemini Blueprint参考文档 第6章 OSGi 4.2 Blueprint Container

来源:互联网 发布:c罗身体数据 编辑:程序博客网 时间:2024/05/17 07:21

Eclipse Gemini Blueprint Reference Guide

http://www.eclipse.org/gemini/blueprint/documentation/reference/1.0.1.RELEASE/html/index.html
原文链接: http://www.eclipse.org/gemini/blueprint/documentation/reference/1.0.1.RELEASE/html/blueprint.html

6 OSGi 4.2 Blueprint Container

Gemini Blueprint编程模型的基础上,OSGi联盟在OSGi 4.2发布版中引入了Blueprint Container规范(作为Compendium Service的一部分)Gemini Blueprint 1.0Blueprint参考实现,这是这个规范的官方的完整实现。

新老用户可以自由混合使用编程模型,自从Eclipse Gemini Blueprint支持Spring DM 1.xBlueprint在同一个应用中同时声明,提供的默认命名空间是BlueprintBlueprint文件位于META-INF/spring文件夹。

请注意本文档主要关注Gemini Blueprint特定的配置和选项。对于Blueprint特性的行为请参考OSGi 4.2 Compendium规范,第121章节。

6.1. Blueprint要求

Blueprint容器规范是OSGi 4.2发布版的一部分,并且依赖它的API。这样,为了使用Blueprint,必须使用OSGi 4.2兼容的平台作为运行时环境。Gemini Blueprint自己只需要OSGi 4.0框架,所以如果4.2不是选项,可以安全的降级到有损的Blueprint模型,这个模型在Spring/Gemini Blueprint上构建。

[Note]

注意

OSGi 4.2之前的环境,Gemini Blueprint会自动禁止Blueprint功能- 用户会收到类似下面的日志通知消息:

Pre-4.2 OSGi platform detected; disabling Blueprint Container functionality

6.2. BlueprintGemini Blueprint的区别

在功能和配置方面,Gemini Blueprint 1.xBlueprint有很多相似点,因为Spring DM是基于Blueprint规范的。除了完全支持Blueprint配置schemaDM 2.x增强了它的声明,提供了允许Blueprint特定行为的选项。下面的表格列出了SpringGemini Blueprint配置与Blueprint之间的最重要的用户差异。本文档中还有一些额外的比较信息。(例如8.2节, “Blueprint Manifest配置比较”9.1.10.2节, “Blueprint服务比较”)。如果需要的话,可以简单的切换两种定义风格。

6.2.1. XML声明

SpringBlueprint大多数的XML声明都是类似的。用Spring命名空间机制,同样的配置可以同时包含SpringGemini Blueprint, Blueprint和其它的命名空间。此外,自定义的元素可以用于实际也可以用于所有的Spring配置元素。(命名空间、声明bean、装饰等待)。下表只关注常用的标准的Spring命名空间和对应的Blueprint对等体。

6.1 XML配置的不同

元素/属性

Gemini Blueprint

Blueprint

命名空间的声明

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/osgi

http://www.osgi.org/xmlns/blueprint/v1.0.0

根元素

<beans>

<blueprint>

默认的延迟

default-lazy

default-activation

默认的初始化方法

default-init-method

-

默认的销毁方法

default-destroy-method

-

默认的自动接入策略

default-autowire, default-autowire-candidates

-

根元素

<beans>

<blueprint>

Bean ID

id

id

Bean Name/Alias

name/<alias>

-

Bean Class

class

class

Bean Scope Name

scope

scope

Built-in Scopes

singleton, prototype, request, session, bundle

singleton, prototype

Lazy Initialization Name/Values

lazy-init=true/false

activation=lazy/eager

依赖

depends-on

depends-on

初始化方法

init-method

init-method

销毁方法

destroy-method

destroy-method

工厂方法

factory-method

factory-method

工厂Bean

factory-bean

factory-ref

Bean继承

parent

-

自动接入策略

autowire, autowire-candidate

-

构造函数

<constructor-arg>

<argument>

属性

<property>

<property>

<value>

<value>

导出服务

<service>

<service>

导入服务

<reference>/<list>/<set>

<reference>/<list>

 

下面的两个配置的功能是等价的:

<?xmlversion="1.0"encoding="UTF-8"?>

<blueprintxmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"default-activation="lazy">

    <beanid="object"class="java.lang.Object"/>

   

    <beanid="length"class="java.lang.Integer">

        <argumentvalue="4"/>

    </bean>

   

    <beanid="buffer"class="java.lang.StringBuffer"depends-on="simple">

        <propertyname="length"ref="length"/>

    </bean>

   

    <beanid="current-time"class="java.lang.System"factory-method="currentTimeMillis"scope="prototype"/>

   

    <beanid="list"class="java.util.ArrayList"destroy-method="clear"activation="eager">

        <argumentref="length"/>

    </bean>

</blueprint>

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"

    default-lazy-init="true">

   

    <beanid="object"class="java.lang.Object"/>

   

    <beanid="length"class="java.lang.Integer">

        <constructor-argvalue="4"/>

    </bean>

   

    <beanid="buffer"class="java.lang.StringBuffer"depends-on="simple">

        <propertyname="length"ref="length"/>

    </bean>

   

    <beanid="current-time"class="java.lang.System"factory-method="currentTimeMillis"scope="prototype"/>

   

    <beanid="list"class="java.util.ArrayList"destroy-method="clear"lazy-init="false">

        <constructor-argref="length"/>

    </bean>

</beans>

正如前面提到的,Gemini Blueprint可以混合使用命令空间,假若默认的命令空间是Blueprint,且Blueprint文件位于META-INF/spring

<blueprintxmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"

  xmlns:beans="http://www.springframework.org/schema/beans"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="

    http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

      

    <beans:beanid="anInteger"class="java.lang.Integer">

      <beans:constructor-argvalue="10"/>

    </beans:bean>

   

    <serviceref="anInteger"interface="java.lang.Comparable"/>

</blueprint>

上面的例子同时使用了Gemini BlueprintSpringbeans命名空间。

6.2.2. 容器能力

从容器的角度看,Blueprint规范将Spring容器的一个子集标准了。下面对此进行了高层次视图比较,不是全面比较:

6.2 容器能力比较

特性

Gemini Blueprint

Blueprint

对象实例化

构造函数实例化

Y

Y

静态工厂实例化

Y

Y

实例工厂实例化

Y

Y

依赖注入

构造函数注入

Y

Y

Setter注入

Y

Y

字段注入

Y

N

方法注入

Y

N

任意方法注入

Y

N

自动接入

Y

N

组件生命周期

延迟初始化

Y

Y

Bean范围

Y

Y

自定义Bean范围

Y

N

内置回调

Y

N

自定义回调

Y

Y

初始化处理

Y

N

 

XML配置来说,由于Gemini BlueprintBlueprint配置转换成了Spring元数据,我们可以依赖Spring特性,而不是Blueprint容器的特性。例如,可以使用Blueprint配置一个bean,同时在这个实例上用注解实现字段注入。同样的对象可以实现Spring的接口,或者依赖其它的后处理器,以实现特定的行为。

注意,本文档还有一些Blueprint的其它信息。这就是说,强烈推荐大家阅读和和使用Blueprint规范作为指导,如果Blueprint容器作为你选择的编程模型。

6.3. 使用Blueprint

Gemini Blueprint不需要额外的jar包或者步骤使能Blueprint功能。这是直接构建到核心中,实际上,Blueprint API是由Gemini Blueprint核心导出的。请阅读下一章节,了解如何安装Gemini BlueprintOSGi compendium规范(第121章节),了解与Blueprint有关的信息,例如引导,配置位置等。对于那些着急的人,简单安装和启动Gemini Blueprintjars (io, core, extender)和它们的依赖(即Springslf4j), 你应该用全集:Gemini Blueprint会自动检测运行环境,启动的bundle类型。