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、接口及其实现类源码

public interface PersonManager {  
    public void savePerson(String username, String password);  
}  
public class PersonManagerImpl implements PersonManager {  
    public PersonManagerImpl(){  
        System.out.println("instance PersonManagerImpl");  
    }  
      
    public void savePerson(String username, String password) {  
        System.out.println("save person: " + username + ", " + password);  
    }  
}  

     2、xml配置文件内容

public interface HelloPerson {  
    public void save(String username, String password);  
}  
public class HelloPersonImpl implements p4.service.HelloPerson {  
    private PersonManager personManager;  
      
    public PersonManager getPersonManager() {  
        return personManager;  
    }  
  
    public void setPersonManager(PersonManager personManager) {  
        this.personManager = personManager;  
    }  
  
    public void save(String username, String password) {  
        personManager.savePerson(username, password);  
    }  
}  

     2、服务消费类

/** 
 * 如果一个Bean对象需要访问BundleContext,则可以让该Bean对象实现BundleContextAware接口 
 */  
public class Activator implements BundleContextAware {  
    private BundleContext bundleContext;  
    private HelloPerson helloPerson;  
      
    public void start() throws Exception {  
        System.out.println("start Activator: " + bundleContext.getBundle().getSymbolicName());  
        helloPerson.save("cjm", "123");  
    }  
      
    public void stop() throws Exception {  
        System.out.println("stop Activator");  
    }  
  
    public void setBundleContext(BundleContext bundleContext) {  
        this.bundleContext = bundleContext;  
    }  
      
    public void setHelloPerson(HelloPerson helloPerson) {  
        this.helloPerson = helloPerson;  
    }  
}  

     3、xml配置文件内容

Xml代码  osgi-Spring DM入门
  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代码  osgi-Spring DM入门
  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