论坛首页 Java企业应用论坛

Blueprint入门之三

浏览 4744 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-01-31  
    在上一篇的例子中,我们在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的那个服务。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics