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

来源:互联网 发布:狼道seo 编辑:程序博客网 时间:2024/06/04 17:56

[置顶] OSGI企业应用开发(六)细说Blueprint & Gemini Blueprint(一)

标签: osgispring企业应用
697人阅读 评论(0)收藏举报
本文章已收录于:
分类:
作者同类文章X
    作者同类文章X

      上篇文章介绍了如何使用Blueprint將spring框架整合到OSGI应用的Bundle中,从上篇文章中我们大概了解了Blueprint与Gemini Blueprint的关系,简单的说,Blueprint是OSGI Alliance借鉴了Spring Dynamic Modules 的思想,为OSGI制定的依赖注入(DI)规范,而Gemini Blueprint的前生是Spring DM,它即兼容之前的Spring DM,又实现了Blueprint规范。本篇文章开始,我们就来了解一下Gemini Blueprint的一些重要使用细节,以及它与Blueprint的差异。

      一、自定义Bean配置文件路径

      上篇文章中,我们知道,使用Blueprint整合Spring后,默认情况下,Gemini Blueprint会从Bundle的META-INF/spring目录下读取Bean的配置文件,而实际上标准的Blueprint规范会从OSGI-INF/blueprint目录下加载Bean的配置文件,为了说明这个观点,我们不妨在篇文章的代码基础上新建一个Bundle来演示。

      首先新建一个Plug-in Project,名称为com.csdn.osgi.helloworld,步骤和前面文章中相同,如果读者不知道如何操作,可以阅读本系列博文的前面几篇,完成后工程如下图所示:
      这里写图片描述
      前面提到过,Gemini Blueprint支持标准的Blueprint规范,所以我们把Bean的配置文件放到OSGI-INF/blueprint目录下也是可以的,为了方便观察Bean是否被实例化,我们可以新建一个Java类,com.csdn.osgi.HelloWorld,内容如下:

      package com.csdn.osgi;public class HelloWorld {    public HelloWorld() {        System.out.println("================Hello World================");    }}
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      如上面代码所示,我们可以在HelloWorld类的构造方法中,输出一些日志信息,这样实例化HelloWorld对象时,控制台中就会打印相应的信息。

      接下来在com.csdn.osgi.helloworld工程中,新建OSGI-INF/blueprint目录,在该目录下新建Bean的配置文件beans.xml,内容如下:

      <?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">    <bean id="helloWorld" class="com.csdn.osgi.HelloWorld"/></blueprint>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

      最后,启动Equinox容器,注意要勾选我们新建的Bundle,名称为com.csdn.osgi.helloworld,如下图所示:
      这里写图片描述
      启动后,控制台输出日志内容如下:

      hello world!十二月 22, 2016 9:44:16 下午 org.eclipse.gemini.blueprint.extender.internal.boot.ChainActivator <init>信息: Blueprint API detected; enabling Blueprint Container functionality十二月 22, 2016 9:44:16 下午 org.eclipse.gemini.blueprint.extender.internal.activator.LoggingActivator start信息: Starting [org.eclipse.gemini.blueprint.extender] bundle v.[2.0.0.M02]osgi> 十二月 22, 2016 9:44:16 下午 org.eclipse.gemini.blueprint.extender.internal.support.ExtenderConfiguration start信息: No custom extender configuration detected; using defaults...十二月 22, 2016 9:44:16 下午 org.springframework.scheduling.timer.TimerTaskExecutor afterPropertiesSet信息: Initializing Timer十二月 22, 2016 9:44:16 下午 org.eclipse.gemini.blueprint.extender.support.DefaultOsgiApplicationContextCreator createApplicationContext信息: Discovered configurations {osgibundle:/META-INF/spring/*.xml} in bundle [Common (com.csdn.osgi.common)]十二月 22, 2016 9:44:17 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml): startup date [Thu Dec 22 21:44:17 CST 2016]; root of context hierarchy十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService信息: Application Context service already unpublished十二月 22, 2016 9:44:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from URL [bundleentry://9.fwk1970436060/META-INF/spring/beans.xml]Hello World!!十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.extender.internal.blueprint.activator.support.BlueprintContainerCreator createApplicationContext信息: Discovered configurations {bundleentry://35.fwk1970436060/OSGI-INF/blueprint/beans.xml} in bundle [Helloworld (com.csdn.osgi.helloworld)]十二月 22, 2016 9:44:17 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=bundleentry://35.fwk1970436060/OSGI-INF/blueprint/beans.xml): startup date [Thu Dec 22 21:44:17 CST 2016]; root of context hierarchy十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService信息: Application Context service already unpublished十二月 22, 2016 9:44:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from OSGi resource[bundleentry://35.fwk1970436060/OSGI-INF/blueprint/beans.xml|bnd.id=35|bnd.sym=com.csdn.osgi.helloworld]十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor stageOne信息: No outstanding OSGi service dependencies, completing initialization for OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml)十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor stageOne信息: No outstanding OSGi service dependencies, completing initialization for OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=bundleentry://35.fwk1970436060/OSGI-INF/blueprint/beans.xml)十二月 22, 2016 9:44:17 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@24259add: defining beans [helloWorld,blueprintBundle,blueprintBundleContext,blueprintContainer,blueprintConverter]; root of factory hierarchy十二月 22, 2016 9:44:17 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b8730e8: defining beans [object,length,buffer,current-time,list]; root of factory hierarchy================Hello World================十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.blueprint.container.support.BlueprintContainerServicePublisher registerService信息: Publishing BlueprintContainer as OSGi service with properties {Bundle-SymbolicName=com.csdn.osgi.helloworld, Bundle-Version=1.0.0.qualifier, osgi.blueprint.container.version=1.0.0.qualifier, osgi.blueprint.container.symbolicname=com.csdn.osgi.helloworld}十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext publishContextAsOsgiServiceIfNecessary信息: Publishing application context as OSGi service with properties {org.eclipse.gemini.blueprint.context.service.name=com.csdn.osgi.helloworld, org.springframework.context.service.name=com.csdn.osgi.helloworld, Bundle-SymbolicName=com.csdn.osgi.helloworld, Bundle-Version=1.0.0.qualifier}十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext publishContextAsOsgiServiceIfNecessary信息: Publishing application context as OSGi service with properties {org.eclipse.gemini.blueprint.context.service.name=com.csdn.osgi.common, org.springframework.context.service.name=com.csdn.osgi.common, Bundle-SymbolicName=com.csdn.osgi.common, Bundle-Version=1.0.0.qualifier}十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.extender.internal.support.DefaultOsgiBundleApplicationContextListener onOsgiApplicationEvent信息: Application context successfully refreshed (OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml))十二月 22, 2016 9:44:17 下午 org.eclipse.gemini.blueprint.extender.internal.support.DefaultOsgiBundleApplicationContextListener onOsgiApplicationEvent信息: Application context successfully refreshed (OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=bundleentry://35.fwk1970436060/OSGI-INF/blueprint/beans.xml))
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46

      日志中输出了================Hello World================,说明我们在OSGI-INF/blueprint目录下配置的bean也能够成功被实例化。

      接下来是本节的重点内容,Gemini Blueprint除了支持从默认的目录加载Bean的配置文件,还支持自定义配置文件的路径,同样支持Spring DM风格和Blueprint风格,首先介绍Spring DM风格。

      有些OSGI基础的朋友应该都知道,OSGI的Bundle与普通Jar包的区别,从文件结构上来说,仅在于META-INF/MANIFEST.MF文件中的描述信息,例如我们新建的com.csdn.osgi.helloworld工程,MANIFEST.MF文件内容如下:

      Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: HelloworldBundle-SymbolicName: com.csdn.osgi.helloworldBundle-Version: 1.0.0.qualifierBundle-Activator: com.csdn.osgi.helloworld.ActivatorBundle-Vendor: CSDNBundle-RequiredExecutionEnvironment: JavaSE-1.8Import-Package: org.osgi.framework;version="1.3.0"
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

      MANIFEST.MF文件由多个信息头构成,具体每一项的含义这里不做过多介绍,读者可以参考OSGI规范和相关的书籍,我们可以使用Gemini Blueprint自定义的Spring-Context信息头来指定配置文件位置,使用方式如下:

      Spring-Context-Value ::= context ( ',' context ) *context ::= path ( ';' path ) * (';' directive) *
      • 1
      • 2
      • 1
      • 2

      上面的表达式描述了Spring-Context的值的形式,是啥意思呢,读者可能看不懂,不要紧,看几个具体的例子就知道了,例如下面的配置方式是允许的:

      Spring-Context: config/account-data-context.xml, config/account-security-context.xml
      • 1
      • 1

      同时指定多个配置文件路径,使用逗号分割开,除此之外,还支持通配符的方式,例如:

      Spring-Context: config/*.xml
      • 1
      • 1

      上面的配置,表示加载config目录下的所有XML文件,另外,我们还可以指定一些属性,例如:

      Spring-Context: config/*.xml;create-asynchronously:=false 
      • 1
      • 1

      参数与配置文件路径使用分号分割开,create-asynchronously参数的含义是application context采用同步还是异步的方式创建,默认为异步方式,其他参数的含义读者可参考Gemini Blueprint官方文档。

      接下来我们可以修改com.csdn.osgi.helloworld工程中的MANIFEST.MF文件,增加Spring-Context信息头来指定Bean的配置文件,例如:

      Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: HelloworldBundle-SymbolicName: com.csdn.osgi.helloworldBundle-Version: 1.0.0.qualifierBundle-Activator: com.csdn.osgi.helloworld.ActivatorBundle-Vendor: CSDNBundle-RequiredExecutionEnvironment: JavaSE-1.8Import-Package: org.osgi.framework;version="1.3.0"Spring-Context: config/*.xml
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

      然后com.csdn.osgi.helloworld工程中新建config/beans.xml文件,内容如下:

      <?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">    <bean id="helloWorld1" class="com.csdn.osgi.HelloWorld"/>    <bean id="helloWorld2" class="com.csdn.osgi.HelloWorld"/></blueprint>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

      然后重启Equinox容器,日志中输出了连续的两行信息,如下:

      ================Hello World================================Hello World================
      • 1
      • 2
      • 1
      • 2

      从输出日志中,可以分析出config/beans.xml中配置的Bean也能够被实例化,说明Spring-Context头指定的Bean配置文件路径生效。

      需要注意的是,Spring-Context指定Bean配置文件路径是Spring DM的方式,而标准的Blueprint规范使用Bundle-Blueprint头来指定Bean配置文件路径,使用方式相同,不做过多介绍,下面是Gemini Blueprint与Blueprint自定义Bean配置文件的区别,如下图所示:
      这里写图片描述

      关于Blueprint与Gemini Blueprint的使用本文暂时介绍这么多,下篇文章继续。

      0
      0
       
       

      我的同类文章

      http://blog.csdn.net
      • Java中(Integer)127 == (Integer)127和(Integer)129 == (Integer)129表达式结果差异分析2017-04-01
      • OSGI企业应用开发(十四)整合Spring、Mybatis、Spring MVC2017-02-12
      • OSGI企业应用开发(十二)OSGI Web应用开发(一)2017-01-20
      • OSGI企业应用开发(十)整合Spring和Mybatis框架(三)2017-01-07
      • OSGI企业应用开发(八)整合Spring和Mybatis框架(一)2016-12-27
      • OSGI企业应用开发系列博文源码托管至Github2017-02-20
      • OSGI企业应用开发(十三)OSGI Web应用开发(二)2017-01-21
      • OSGI企业应用开发(十一)Bundle资源获取途径2017-01-15
      • OSGI企业应用开发(九)整合Spring和Mybatis框架(二)2017-01-07
      • OSGI企业应用开发(七)细说Blueprint & Gemini Blueprint(二)2016-12-25
      更多文章
      0 0
      原创粉丝点击