`
killko
  • 浏览: 104032 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
Group-logo
Servicemix&Fu...
浏览量:0
社区版块
存档分类
最新评论
阅读更多
    在上一篇的例子中,我们在bean节点里用property注入了一个OSGI service引用。那么这个OSGI service是从哪里来的呢?下面我们介绍一下Blueprint如何注册一个OSGI service。

    首先我们在java代码中定义一个interface,然后用一个类去实现它,就和面向接口开发一样。最后,我们就通过Blueprint的service节点发布这个OSGI service。
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd" default-timeout="0">
    <!--最简单的osgi服务注册 -->
    <bean id="coder" class="com.ponder.CoderImpl"/>
    <service id="CoderService" ref="coder" interface="com.ponder.ICoder"/>
    <!--Embeded bean的osgi服务注册 -->
    <service id="CoderService2" interface="com.ponder.ICoder">
    	<bean class="com.ponder.CoderImpl"/>
    </service>
    <!--带osgi服务属性的服务-->
    <service id="CoderService3" interface="com.ponder.ICoder">
    	<service-properties>
    		<entry key="param01" value="val01"/>
    		<entry key="param02" value="val02"/>
    		<entry key="param03">
        	<array value-type="java.lang.String">
          	<value>val03-1</value>
            <value>val03-2</value>
            <value>val03-3</value>
            </array>
        </entry>
    	</service-properties>
    	<bean class="com.ponder.CoderImpl"/>
    </service>
    <!-- 没将接口抽离出来的osgi服务,会将com.ponder.CoderImpl里的所有public方法都作为服务的方法 -->
    <service id="CoderService3"  interface="com.ponder.CoderImpl">
    	<bean class="com.ponder.CoderImpl"/>
    </service>
</blueprint>

     在上例中,我们举了三个发布OSGI service的例子,最简单的例子就先用bean节点定义那个服务的实现类,并给这个bean定义一个id(“coder”),然后在service节点用interface属性指明对应的接口,并用ref属性来引用刚才定义的那个bean。这样Blueprint container就可以往OSGI framework里注册发布这个服务了。

    第二个例子中,则是将bean节点直接嵌入service节点中,而不需用bean的id和service节点属性ref了。

    第三个例子则给service添加了service-property,这个service-property和bean节点的property不同,它不出现在java代码里,只是在Blueprint往OSGI framework里注册这个服务时,以map的形式附带上这些service-property。这些service-property可以在服务被引用时,作为filter的条件。

    服务被发布后,在Blueprint里,可以用reference节点来引用用这些服务。

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd" default-timeout="0">
    <!--引用osgi服务,并注入bean(com.ponder.Processor)里 -->
    <reference id="coderService" filter=”(param01=val02)” interface="com.ponder.ICoder" timeout="0"/>
    <bean id="processor" class="com.ponder.Processor">
    	<!--与这里对应,类com.ponder.Processor里应定义有以下属性:
    	private com.ponder.ICoder coder;
    	并包含其setter。
    	-->
    	<property name="coder" ref="coderService"/>
    </bean>
    
</blueprint>


请留意上例reference节点中的filter:OSGI framework里可以有多个实现相同interface的服务,这里的filter就限定了这个reference节点只引用包含一个名为param01,而且值为val01的service-property的那个服务。
分享到:
评论

相关推荐

    osgi gemini blueprint环境

    标题中的“osgi gemini blueprint环境”指的是OSGi(Open Services Gateway Initiative)框架下的Gemini Blueprint服务。OSGi是一个模块化系统,它允许Java应用程序被分解为一系列可独立部署的模块,增强了软件的可...

    OSGI资料,OSGI进阶,OSGI实战,OSGI入门和整合Spring

    3. **Blueprint**:Spring的OSGI扩展,提供类似Spring XML配置的模块化服务定义方式。 4. **Aries SPI Fly**:一种用于将Spring应用转换为OSGI bundle的工具,保持原有Spring配置不变。 《OSGI_Opendoc.rar》可能...

    Spring OSGI 快速入门中文教程

    - **Blueprint** 和 **Declarative Services** 是两种常见的Spring OSGi服务配置方式,它们都允许在OSGi中声明性地定义Bean和服务。 3. **Spring OSGi的应用场景** - **动态性**:OSGi环境下的Spring应用能更好地...

    OSGI入门和整合Spring

    3. **使用Declarative Services(DS)**:OSGI DS提供了一种声明式的方式来管理OSGI服务的生命周期,使得Spring的bean可以与OSGI服务无缝集成。 4. **动态依赖注入**:由于OSGI的动态性,服务可以在运行时添加或...

    spring-osgi 入门手册和代码

    3. **开始使用 Spring OSGi** - **环境准备**:安装一个 OSGi 容器,如 Apache Felix 或 Equinox,这些都是 Spring OSGi 支持的容器。 - **创建 bundle**:将 Spring 应用打包为 OSGi bundle,确保所有依赖都被...

    OSGi 入门+进阶+实战

    5. **Blueprint或Declarative Services**:这两种是OSGi中的服务配置方式,Blueprint更接近XML,而Declarative Services使用注解,简化了服务的声明和管理。 6. **远程服务**:OSGi Remote Services允许Bundle之间...

    Spring OSGi 入门.pdf

    **Spring OSGi 入门** Spring OSGi 是 Spring 框架与开放服务网关规范(OSGi)的结合,它为基于 Java 的应用程序提供了模块化开发的能力。OSGi 是一个动态的、模块化的运行时环境,使得开发者可以创建可热插拔的...

    精彩:OSGI入门以及提升

    相关技术文章可能深入探讨了OSGI的高级特性,例如: Blueprint Container用于在OSGI环境中实现依赖注入,它简化了服务的创建和管理;felix-configadmin服务允许动态配置bundle;还有OSGI的远程服务(Remote Services...

    OSGISpring OSGISpring

    7. **Blueprint服务**:OSGI的Blueprint规范提供了一种在OSGI环境中声明式地定义服务和依赖的XML格式,与Spring的XML配置类似,但更适合OSGI环境。 8. **冲突管理**:在OSGI环境中,多个bundle可能提供相同的服务,...

    tomcat-osgi压缩包

    这些bundle可以是第三方库、应用程序模块或者系统服务,通过声明式服务(Declarative Services,DS)或 Blueprint等配置机制,来实现服务间的依赖管理和通信。 在提供的压缩包文件名称“Tomcat-OSGi-QuickStart”中...

    osgi相关文档、及学习资料

    可能会包括如何创建和打包OSGi Bundle、配置Manifest文件(包含Bundle的元数据)、使用Blueprint或Declarative Services进行服务声明、理解OSGi的生命周期管理以及如何在实际项目中应用OSGi技术。 **OSGi原理与最佳...

    karaf 手册和入门资料

    ### Karaf 手册与入门指南 ...通过上述介绍,我们可以看到 Karaf 作为一个轻量级的 OSGi 运行时,不仅具备了丰富的功能和灵活性,还为开发者提供了强大而方便的工具集,使其成为构建复杂应用的理想平台之一。

    spring-dm-reference

    - **OSGi 4.2 Blueprint Reference Implementation (RI)**: 实现了最新的 OSGi Blueprint 规范。 - **Java 5**: 兼容 Java 5 及以上版本。 - **Spring 3.x**: 兼容 Spring 3.x 版本。 - **服务导入改进**: 增强了...

    快速入门指南 中文版 ServiceMix4

    输出包括bundle的ID、状态、Blueprint或Spring XML文件的bean创建情况、启动级别以及名称和版本。通过管道和命令行工具,可以方便地搜索特定的bundle,例如查找与"Camel"相关的bundle。 4. **使用Apache ServiceMix...

    CAMEL核心开发指南

    - **Apache Karaf**:这是一个小型的基于OSGi的运行时环境,允许部署应用程序。 - **Apache MINA**:这是一个高性能的、基于NIO的网络框架。 4. **集成模式**: CAMEL支持多种集成模式,这些模式定义了企业应用...

Global site tag (gtag.js) - Google Analytics