在上一篇的例子中,我们在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的入门资料,网上找的,初探OSGi 的全文
OSGi的入门教程,帮助初学者快速了解OSGi的定义、用途及组成。
强大的OSGI框架入门和例子,看看eclipse就知道OSGI有多强大了。希望对大家有用!
OSGI 入门资料PDF,包括OSGI实战、OSGI进阶、OSGI 入门和整合Spring、OSGI原理与最佳实践
osgi-blueprint-helloworld 概述 一个使用OSGI蓝图容器规范的非常简单的HelloWorld示例。 想要查询更多的信息: osgi-blueprint-helloworld-api 应用程序API osgi-blueprint-helloworld-server 发布服务以供...
NULL 博文链接:https://conkeyn.iteye.com/blog/2113824
Spring OSGI 快速入门中文教程
OSGI简单入门.doc
osgi webservice入门
OSGI入门和整合Spring OSGI入门和整合Spring
spring-osgi 入门手册和代码 spring-osgi 入门手册和代码 spring-osgi 入门手册和代码 spring-osgi 入门手册和代码
总结了osgi服务发布和获取的多种方式。
Equinox这个工程,对Equinox进行了试用,不出意料,尽管Equinox还没有正式的发布版本,甚至连里程碑版本都还没有,但Equinox作为OSGI R4 RI,表现非常出色,特别是借助Eclipse提供的IDE,更是令它在OSGI框架的竞争中...
Spring OSGi 入门.pdf Spring OSGi 入门.pdf
OSGI入门和整合Spring + OSGI进阶 + OSGI实战
OSGi 入门级教程OSGi 入门级教程OSGi 入门级教程OSGi 入门级教程中文版
blueprint-osgi-bundle
关于SOA中热门技术OSGI的入门样例的经典版本,供对需要进行ERP设计和学习的入门读者使用的经典
资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...