`
snoopy7713
  • 浏览: 1124266 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

RAP 整合 Spring (基于 Spring-osgi )

    博客分类:
  • RAP
阅读更多

RAP 介绍请见: http://www.eclipse.org/rap/

Spring 介绍请见: http://www.springsource.org/

Spring-osgi 介绍请见: http://www.springsource.org/osgi

 

RAP 也是完全的基于 OSGI ,只要对于 osgi 的概念以及 Spring-osgi 的具体用法能够完全理解的话,那么这里所谓的 RAP 整合 Spring ,就和网上常见的 OSGI 整合 Spring 没什么大的区别了。

 

Spring-osgi 就是在 Spring 的基础上,使得在 osgi 环境下也能够使用 Spring osgi 下单 bundle 有其自己的生命周期, Spring-osig 需要相互注入分布在多个 bundle 间的 bean ,而这些 bean 又和自身所在的 bundle 拥有一致的生命周期。那么 Spring-Osgi 需要解决的难题正是 classloader 问题,以及将分布在不同生命周期的 bundle bean 以一定的模式整合在一起(也就是所谓的注入, bean 的依赖关系)。

 

第一步 :下载 Spring ,下载 Spring-osgi

       将下载的 Spring-osig 下的 dist lib 下的所有 jar 放到 Eclipse RAP target 下。

      

       需要注意的问题 Spring-osgi lib 内包含了一个 org.eclipse.osgi 包,同时 Eclipse RAP target 下也包含了一个 org.eclipse.osgi 包,由于 Spring-osig 提供的 org.eclipse.osgi 版本更新,你可以将原来 EclipseRap Target 下的 org.eclipse.osgi 包删除(也就是选择版本低的删除即可)。     

 

第二步 :创建基本的 RAP 项目

使用 EclipseRAP 提供的模板创建一个 mail 的项目。

需要注意的问题:需要选择基于 Eclipse ,添加 Activator ,这样点击‘下一步‘后,才可以选择 RAP 提供的几个模板来创建项目。

 

第三步 :创建 server 项目,创建 server.impl 项目

这里所谓的 server 项目,指的是: model bo vo ), service interface

server.impl 项目,指的是: service interface 的实现。

 

server 项目内容比较少,具体内容见代码:

public interface IDemoService {

    public void sayHello();

}

 

server.impl 项目中 .具体内容见代码:

public class DemoService implements IDemoService {

    public void sayHello() {

       System. out .println( "Hello OSGI" );

    }

}

 

 

META-INF 文件下为 spring 创建一个 xml 文件 (xml 文件名称任意 ).

       具体内容见代码 :

    < bean id = "demoService" scope = "bundle" class = "lggege.rap.demo.server.impl.DemoService" >

    </ bean >

    < osgi:service ref = "demoService" interface = "lggege.rap.demo.server.IDemoService" />

 

第四步 RAP 调用 implement 中的服务

<!---->1.       <!----> launch 中的 Activator BundleContext 对外暴露,具体内容见代码:

public class Activator extends AbstractUIPlugin {

    private BundleContext context ;

    public void start(BundleContext context) throws Exception {

       super .start(context);

       plugin = this ;

       this . context = context;

    }

    public BundleContext getContext() {

       return context ;

    }

    // .. 省略其他代码

}

 

<!---->2.       <!---->这步就比较简单了,就是在 RAP 的某个控件的添加一个 listener ,通过从 bundleContext 中使用 Service interface class 来获得服务。

       我是为 MessagePopupAction 添加了 listener, 具体内容见代码:

 

    public void run() {

//        MessageDialog.openInformation(window.getShell(), "Open", "Open Message Dialog!");

    ServiceReference serviceReference = Activator.getDefault ().getContext().getServiceReference(IDemoService. class .getName());

    IDemoService demoService = (IDemoService) Activator.getDefault ().getContext().getService(serviceReference);

    demoService.sayHello();

}

 

第五步 :运行

       需要在 run 中添加 spring-osgi 对应的几个 bundle 即可。进入 RAP 后,点击特定的控件去触发事件,通过在事件中调用外部的服务。

       具体效果:控制台看到信息输出 .

 

第六步 :分析

就这么简单,通过使用 Spring-osgi ,我们能够通过配置的方式往 osgi 注册服务,这个服务是来自于 Spring 配置的 bean RAP 通过从 BuncleContext Service interface 来获得服务。

 

 

后续文章的内容:

RAP 整合 Spring (基于 Spring-osgi )可下载源码和视频 URL:http://lggege.iteye.com/blog/314666
spring-osgi配置log4j(创建fragment project) 可下载视频 URL:http://lggege.iteye.com/blog/314874
Spring-osgi整合iBATIS 可下载代码和视频 URL:http://lggege.iteye.com/blog/315257

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics