osgi-Spring DM入门

转自: 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值中的指令主要有以下这些:

扫描二维码关注公众号,回复: 525824 查看本文章

      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 

猜你喜欢

转载自coffeehot.iteye.com/blog/2104126
dm
今日推荐