`

osgi-Spring DM入门

    博客分类:
  • esb
 
阅读更多

转自: http://chenjumin.iteye.com/blog/819070

Spring Dynamic Modules (即Spring动态模型,简称Spring DM)允许开发者构建Spring应用程序,这种应用程序能够在OSGi容器中进行部署,并可有效利用OSGi框架所提供的服务。这种应用程序具有以下几方面的优点:

      1、更好的分离应用逻辑与模块。
      2、同时部署同一个模块的不同版本的能力。
      3、动态发现和使用系统内其他模块提供的服务的能力。
      4、在运行着的系统中动态地安装、更新和卸载模块的能力。
      5、使用 Spring 框架在模块内部和模块之间进行实例化、配置、整合组件的能力。
      6、让企业应用开发者使用简单、熟悉的编程模型开发OSGi平台的功能。

 

Spring DM很关键的一个jar文件是spring-osgi-extender-*.*.*.jar,它主要负责为Bundle实例化Spring应用程序上下文。extender可以通过两种方式来识别需要处理的Bundle,一是MANIFEST.MF文件里包含了Spring-Context 头条目的Bundle,二是extender将把META-INF/spring下面的所有XML文件视为有效Spring 配置文件。缺省情况下,Spring使用META-INF/spring目录下所有的xml文件来创建Application Context。缺省设置可以在Spring-Context的manifest header中重写,Header的值是由逗号分隔的资源路径及指令列表表示。

 

Spring-Context头条目的配置范例如下:

      Spring-Context: config/applicationContext.xml, config/beans-security.xml 
      Spring-Context: *;create-asynchronously=false
      Spring-Context: config/osgi-*.xml;wait-for-dependencies:=false
      Spring-Context: *;timeout:=60 
      Spring-Context: *;publish-context:=false

 

 

Header值中的指令主要有以下这些:

      create-asynchronously (true|false):控制是否异步地(默认)或同步地创建应用程序上下文。
      wait-for-dependencies  (true|false):控制在上下文创建之前是否要等待(默认)或不等待所有强制依赖的服务变成 satisfied。
      timeout (300):等待强制依赖服务变成 satisfied 的时间(以秒为单位),超时之后放弃等待且上下文创建会失败。如果 wait-for-dependencies:=false 被指定,那么这个设置会被忽略。默认值是 5 分钟(300 秒)。 
      publish-context  (true|false):控制是否应用程序上下文对象是否发布其自身到 OSGi 服务注册表中。默认是发布。

 

一、将Bean输出为OSGi服务

     1、接口及其实现类源码

Java代码  收藏代码
  1. public interface PersonManager {  
  2.     public void savePerson(String username, String password);  
  3. }  

 

Java代码  收藏代码
  1. public class PersonManagerImpl implements PersonManager {  
  2.     public PersonManagerImpl(){  
  3.         System.out.println("instance PersonManagerImpl");  
  4.     }  
  5.       
  6.     public void savePerson(String username, String password) {  
  7.         System.out.println("save person: " + username + ", " + password);  
  8.     }  
  9. }  

 

     2、xml配置文件内容

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:osgi="http://www.springframework.org/schema/osgi"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.     http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">  
  8.       
  9.     <!--  
  10.         bundle:为每一个服务的导入者生成一个新的服务实例。当服务导入者停止时,服务实例会被回收。 
  11.      -->  
  12.     <bean id="personManager" scope="bundle" class="p3.service.impl.PersonManagerImpl"/>  
  13.       
  14.     <!--  
  15.         自动将受管Bean暴露成OSGi服务,并注册到OSGi容器中。不需要借助BundleActivator对象。 
  16.      -->  
  17.     <osgi:service id="personManagerService" ref="personManager" interface="p3.service.PersonManager"/>  
  18.       
  19. </beans>  

      被Service元素定义的Bean的类型是org.osgi.framework.ServiceRegistration,它是在OSGi服务注册表中注册输出Bean而产生的ServiceRegistration对象。

      指定服务接口(或者服务接口集):
            使用interface属性来指定一个全限定接口名
            使用嵌套的interfaces元素可以指定多个服务接口
                  <osgi:service id="personManagerService" ref="personManager">
                        <osgi:interfaces>
                              <value>p3.service.PersonManager</value>
                        </osgi:interfaces>
                  </osgi:service>

 

     3、MANIFEST.MF文件内容

        必须导出接口所在的包(如p3.service)供其它Bundle引入

Xml代码  收藏代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: P3 Plug-in  
  4. Bundle-SymbolicName: p3  
  5. Bundle-Version: 1.0.0  
  6. Import-Package: org.osgi.framework;version="1.3.0"  
  7. Export-Package: p3.service;version="1.0.0"  
  8. Bundle-ClassPath: bin/  

 

二、引用由Spring Bean输出的OSGi服务  

     1、接口及其实现类 

Java代码  收藏代码
  1. public interface HelloPerson {  
  2.     public void save(String username, String password);  
  3. }  

 

Java代码  收藏代码
  1. public class HelloPersonImpl implements p4.service.HelloPerson {  
  2.     private PersonManager personManager;  
  3.       
  4.     public PersonManager getPersonManager() {  
  5.         return personManager;  
  6.     }  
  7.   
  8.     public void setPersonManager(PersonManager personManager) {  
  9.         this.personManager = personManager;  
  10.     }  
  11.   
  12.     public void save(String username, String password) {  
  13.         personManager.savePerson(username, password);  
  14.     }  
  15. }  

 

     2、服务消费类

Java代码  收藏代码
  1. /** 
  2.  * 如果一个Bean对象需要访问BundleContext,则可以让该Bean对象实现BundleContextAware接口 
  3.  */  
  4. public class Activator implements BundleContextAware {  
  5.     private BundleContext bundleContext;  
  6.     private HelloPerson helloPerson;  
  7.       
  8.     public void start() throws Exception {  
  9.         System.out.println("start Activator: " + bundleContext.getBundle().getSymbolicName());  
  10.         helloPerson.save("cjm""123");  
  11.     }  
  12.       
  13.     public void stop() throws Exception {  
  14.         System.out.println("stop Activator");  
  15.     }  
  16.   
  17.     public void setBundleContext(BundleContext bundleContext) {  
  18.         this.bundleContext = bundleContext;  
  19.     }  
  20.       
  21.     public void setHelloPerson(HelloPerson helloPerson) {  
  22.         this.helloPerson = helloPerson;  
  23.     }  
  24. }  

 

     3、xml配置文件内容

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:osgi="http://www.springframework.org/schema/osgi"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.     http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">  
  8.       
  9.     <!--   
  10.         reference元素:用于定义一个本地bean代理某个OSGi服务(即引用其它Bundle导出的服务)  
  11.             id:本地bean的名字  
  12.             interface:目标服务所注册的接口的全路径名  
  13.      -->  
  14.     <osgi:reference id="personManagerOSGI" interface="p3.service.PersonManager" />  
  15.   
  16.     <bean id="helloPerson" scope="bundle" class="p4.service.impl.HelloPersonImpl">  
  17.         <property name="personManager" ref="personManagerOSGI"/>  
  18.     </bean>  
  19.       
  20.     <bean id="activator" class="p4.Activator" init-method="start" destroy-method="stop">  
  21.         <property name="helloPerson" ref="helloPerson"></property>  
  22.     </bean>  
  23. </beans>  

 

     4、MANIFEST.MF文件内容

Xml代码  收藏代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: P4 Plug-in  
  4. Bundle-SymbolicName: p4  
  5. Bundle-Version: 1.0.0  
  6. Import-Package: org.osgi.framework;version="1.3.0",  
  7.  p3.service;version="1.0.0"  
  8. Require-Bundle: org.springframework.bundle.osgi.core 
分享到:
评论

相关推荐

    spring osgi 入门

    ### Spring OSGi 入门知识点详解 #### 一、Spring-DM与OSGi结合的优势 Spring Dynamic Modules (Spring DM) 是Spring Framework的一个扩展项目,它使得Spring可以在OSGi环境中运行,进而为开发者提供了模块化的...

    Spring OSGi 入门.pdf

    ### Spring OSGi 入门知识点...以上内容为Spring OSGi入门的基本知识点,涵盖了Spring DM的基础概念、配置方法以及如何在OSGi环境中导出和引用服务等内容。这些知识点对于理解如何将Spring与OSGi结合使用具有重要意义。

    spring-dm-reference

    Spring DM 主要用于简化在 OSGi 平台上开发 Spring 应用程序的过程,并提供了许多高级功能来支持动态模块化应用。 ##### 为什么选择 Spring Dynamic Modules? 1. **集成性**: Spring DM 提供了一个强大的集成平台...

    Apress - Pro Spring Dynamic Modules for OSGi Service Platforms 2009

    2. Spring DM入门:学习如何配置Spring DM,创建OSGi bundle,并在其中定义Spring配置。 3. 服务发现和通信:探讨如何在OSGi环境中查找和使用服务,包括使用服务引用和服务事件。 4. 动态性与生命周期:了解如何在...

    OSGI入门和整合Spring

    学习OSGI入门和整合Spring,对于开发复杂的企业级应用,或者想要提升系统灵活性和可维护性的开发者来说,是非常有价值的。通过理解OSGI的模块化机制和Spring的依赖注入原理,可以构建出更加高效和可扩展的Java应用。

    Spring DM IN ACTION

    - **Spring DM**(Spring Dynamic Modules)是Spring框架的一个扩展,它结合了OSGi(Open Service Gateway Initiative)的强大功能,旨在帮助开发者在复杂的分布式环境中构建更加灵活、可扩展的应用程序。...

    OSGI 入门资料PDF

    Spring Dynamic Modules(Spring-DM)是用于OSGI的Spring扩展,它使得Spring应用可以无缝地在OSGI环境中运行。通过Spring-DM,你可以利用Spring的依赖注入和配置管理能力,同时享受OSGI的模块化和动态性。 5. OSGI...

    Spring OSGI 快速入门中文教程

    1. **环境搭建**:首先需要安装一个支持OSGi的运行时环境,如Apache Felix或Equinox,然后配置Spring OSGi容器,如Spring DM(现在已改名为SpringSource dm Server)。 2. **编写bundle**:创建Spring配置文件,定义...

    spring dm cxf

    2. **Spring DM入门**:讲解如何配置Spring DM,设置OSGi环境,以及如何在OSGi容器中启动和管理Spring应用。 3. **Apache CXF集成**:详细阐述如何在Spring DM环境中引入和配置CXF,以便创建和暴露Web服务。可能...

    OSGI原理最佳实践(包含源代码)

    是一本适合新接触OSGI开发学习的一本很好的书,本书介绍了Equinox, Spring-DM和Felix这三个常用的OSGi容器的使用、开发、以及WebApplication的开发部署;介绍了OSGi的规范和Core Framework和Layer。包含书中的用例源...

    OSGi原理与最佳实践

    本书基于作者多年使用OSGi的经验而编写,涵盖了...最后对OSGi知识进行深入讲解,通过对OSGi规范和实现框架(Equinox、Felix、Spring-DM和Apache CXF)的分析,以及最佳实践的介绍,帮助读者更好地掌握如何使用OSGi。

    精彩:OSGI入门以及提升

    此外,你可能会接触到OSGI在企业级应用中的应用,如Spring DM(现在称为Spring OSGi)如何与OSGI结合,提供了在OSGI环境中使用Spring框架的能力。OSGI还常用于嵌入式系统,因为它允许在资源受限的环境中高效地管理和...

    EclipseMavenSpringDM(译文)- 中文版

    虽然本指南不会深入讲解OSGi技术细节或Spring DM的具体应用,但它为初学者提供了快速入门的路径,并提供了必要的资源链接供进一步学习。 #### 第2章 先决条件 在开始之前,开发者需要准备一些基本的开发工具。具体...

    Spring Dynamic Modules 实战(中文版)

    ### Spring Dynamic Modules 实战(中文版) #### 知识点概述 Spring Dynamic Modules...开发者通过本实战文档可以快速入门并掌握在OSGi平台上利用Spring框架开发应用程序的技能,提升个人和企业的开发效率和应用质量。

Global site tag (gtag.js) - Google Analytics