上篇文章中,我们开发了一个自定义的Bundle,接着从网络中下载到Spring和Blueprint的Bundle,然后复制到DynamicRuntime项目下。

需要注意的是,这些Bundle并不能在Eclipse自带的Equinox启动管理工具中显示,因为我们并没有將这些Bundle添加到自定义的Target Platform中,接下来我们就来完成这一操作。

一、启动Spring和Blueprint相关Bundle

单击Window=>Preferences菜单,然后单击Plug-in Development=>Target Platform选项,选择我们之前新建的DynamicRuntime,如下图所示:



单击Edit按钮,按照前面文章中提到的方式,把spring 和blueprint目录下的Bundle添加到DynamicRuntime中,如下图:



然后单击Finish按钮即可完成操作,接下来我们要做的是启动Spring和Blueprint相关的Bundle。

单击Run=>Debug Configurations…菜单,单击OSGI Framework下面我们之前的新建的启动项,如下图:



我们需要勾选Spring和Blueprint相关的全部Bundle,然后单击面板上的Validate Bundles按钮,该功能用于校验Bundle是否存在依赖问题,单击后,弹框内容如下:



可以发现Spring相关的Bundle都缺少公共的日志包依赖,普通的Java项目对应的common-logging.jar包,我们可以单击Add Required Bundles按钮,让Eclipse工具自动为我们勾选上依赖的Bundle,然后再次单击Validate Bundles按钮,出现如下弹窗说明不存在依赖问题了:



这是一个非常好的现象,意味着我们可以重新启动Equinox容器了,单击Debug按钮即可,控制台输出日志内容如下:

Hello World!!
十二月 18, 2016 10:32:34 下午 org.eclipse.gemini.blueprint.extender.internal.boot.ChainActivator <init>
信息: Blueprint API detected; enabling Blueprint Container functionality
十二月 18, 2016 10:32:34 下午 org.eclipse.gemini.blueprint.extender.internal.activator.LoggingActivator start
信息: Starting [org.eclipse.gemini.blueprint.extender] bundle v.[2.0.0.M02]
osgi> 十二月 18, 2016 10:32:34 下午 org.eclipse.gemini.blueprint.extender.internal.support.ExtenderConfiguration start
信息: No custom extender configuration detected; using defaults...
十二月 18, 2016 10:32:34 下午 org.springframework.scheduling.timer.TimerTaskExecutor afterPropertiesSet
信息: Initializing Timer

二、通过XML文件配置Bean

到目前为止,我们已经成功使用Blueprint和Spring整合到OSGI中,但是好像还缺点什么,在普通的Java项目中,我们通过ApplicationContext相关子类启动Spring容器,然后加载相关的Bean配置文件。但是在OSGI应用中Spring框架的启动由Blueprint负责,默认情况下会加载META-INF/spring/*.xml文件,并实例化这些配置文件中的Bean。

需要注意的是Gemini Blueprint支持Blueprint风格和Spring风格的Bean配置,接下来我们可以在com.csdn.osgi.common工程的META-INF目录下新建一个spring目录,然后新建一个beans.xml文件,如下图:



然后在beans.xml文件中增加Blueprint风格的Bean配置,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-activation="lazy">
<bean id="object" class="java.lang.Object"/> <bean id="length" class="java.lang.Integer">
<argument value="4"/>
</bean> <bean id="buffer" class="java.lang.StringBuffer" depends-on="simple">
<property name="length" ref="length"/>
</bean> <bean id="current-time" class="java.lang.System" factory-method="currentTimeMillis" scope="prototype"/> <bean id="list" class="java.util.ArrayList" destroy-method="clear" activation="eager">
<argument ref="length"/>
</bean>
</blueprint>

然后重新启动Equinox容器,控制台中输出日志内容如下:

hello world!
十二月 18, 2016 10:50:57 下午 org.eclipse.gemini.blueprint.extender.internal.boot.ChainActivator <init>
信息: Blueprint API detected; enabling Blueprint Container functionality
十二月 18, 2016 10:50:57 下午 org.eclipse.gemini.blueprint.extender.internal.activator.LoggingActivator start
信息: Starting [org.eclipse.gemini.blueprint.extender] bundle v.[2.0.0.M02]
osgi> 十二月 18, 2016 10:50:57 下午 org.eclipse.gemini.blueprint.extender.internal.support.ExtenderConfiguration start
信息: No custom extender configuration detected; using defaults...
十二月 18, 2016 10:50:57 下午 org.springframework.scheduling.timer.TimerTaskExecutor afterPropertiesSet
信息: Initializing Timer
十二月 18, 2016 10:50:57 下午 org.eclipse.gemini.blueprint.extender.support.DefaultOsgiApplicationContextCreator createApplicationContext
信息: Discovered configurations {osgibundle:/META-INF/spring/*.xml} in bundle [Common (com.csdn.osgi.common)]
十二月 18, 2016 10:50:57 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml): startup date [Sun Dec 18 22:50:57 CST 2016]; root of context hierarchy
十二月 18, 2016 10:50:57 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService
信息: Application Context service already unpublished
十二月 18, 2016 10:50:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [bundleentry://9.fwk182531396/META-INF/spring/beans.xml]
十二月 18, 2016 10:50:58 下午 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)
十二月 18, 2016 10:50:58 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1103899a: defining beans [object,length,buffer,current-time,list]; root of factory hierarchy
十二月 18, 2016 10:50:58 下午 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}
十二月 18, 2016 10:50:58 下午 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))

日志内容说明Spring框架已经成功启动,并且Bean已经成功创建。

除此之外,Gemini Blueprint还支持Spring风格的Bean配置,例如我们可以將上面的beans.xml文件内容修改成下面的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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"> <bean id="object" class="java.lang.Object"/> <bean id="length" class="java.lang.Integer">
<constructor-arg value="4"/>
</bean> <bean id="buffer" class="java.lang.StringBuffer" depends-on="simple">
<property name="length" ref="length"/>
</bean> <bean id="current-time" class="java.lang.System" factory-method="currentTimeMillis" scope="prototype"/> <bean id="list" class="java.util.ArrayList" destroy-method="clear" lazy-init="false">
<constructor-arg ref="length"/>
</bean>
</beans>

重新启动Equinox容器也是没有问题的,Spring框架能够正常启动,Bean也能够正常实例化,到此为止,我们已经成功使用Blueprint整合到Spring框架中。

这篇文章暂时就介绍这么多,也到休息时间了,后面的文章中继续介绍如何与Mybatis框架和Spring MVC整合,并开发一个简单的登录功能。如果发现文章内容有不当的地方,希望能够指出来,有任何疑问也可以在评论中提出,转载请注明本文地址!

本文源码下载地址:http://download.csdn.net/detail/rongbo_j/9715778

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框架(一)

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

  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. 【xsy1012】KSHKM的基因工程 AC自动机DP

    题目大意:给你$n$个串$p_i$,最后再给一个串$s$(字符集均为A,C,G,T四个字符中的一个).问你串$s$最少要更改多少个字符(更改后的字符也只能是ACGT),才能满足s中不包含$p_i$$( ...

  2. [原创]K8 DNN密码解密工具(DotNetNuke Password Decrypt)

    工具: K8_DNN_Password_Decrypt编译: VS2012  C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http: ...

  3. rabbitmq系列五 之远程过程调用(RPC)

    1.远程过程调用(RPC) 在第二篇教程中我们介绍了如何使用工作队列(work queue)在多个工作者(woker)中间分发耗时的任务. 可是如果我们需要将一个函数运行在远程计算机上并且等待从那儿获 ...

  4. Spring框架的演变

    什么是Spring 如果想要解释Spring,那么最难的部分就是对其进行分类.通常情况下,Spring被描述为构建Java应用程序的轻量级框架,但这种描述带来了两个有趣的观点. 首先,与许多其他框架( ...

  5. C#简单操作MongoDB

    一 安装MongoDB 官网按需下载, 安装, 一步到位. 二 VS创建新项目 创建一个.netcore console项目, 然后nuget安装驱动MongoDB.Driver 三 建立连接 在Pr ...

  6. 剑指offer十七之树的子结构

    一.题目 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 二.思路 1.首先设置标志位result = false,因为一旦匹配成功result就设为tru ...

  7. java中this$0 this$1 this$2

    import java.lang.reflect.Field; public class Outer {//this$0 public class FirstInner {//this$1 publi ...

  8. docker-使用ali云加速

    ali专用网络加速地址; https://yq.aliyun.com/articles/29941 使用ali加速 vim /etc/sysconfig/docker 添加: ADD_REGISTRY ...

  9. AR介绍

    AR介绍 AR全名扩增实境,是一种实时融合现实与虚拟的图像技术. AR技术的三板斧:感知(寻找目标定位位置-与环境交互),渲染(实现产品交互-与客户交互),追踪(捕捉目标运动轨迹-客户环境上下文). ...

  10. 软件架构设计学习总结(15):远程通信(RPC,Webservice,RMI,JMS、EJB、JNDI的区别)对比

    总结这些概念都是易混淆,最基本概念定义复习和深入理解,同时也是架构师必备课程   RPC(Remote Procedure Call Protocol) RPC使用C/S方式,采用http协议,发送请 ...