OSGI Buleprint(3)

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://killko.blog.51cto.com/3419292/1131103

 在上一篇的例子中,我们在bean节点里用property注入了一个OSGI service引用。那么这个OSGI service是从哪里来的呢?下面我们介绍一下Blueprint如何注册一个OSGI service。 

    首先我们在java代码中定义一个interface,然后用一个类去实现它,就和面向接口开发一样。最后,我们就通过Blueprint的service节点发布这个OSGI service。 

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.     <!--最简单的osgi服务注册 -->  
  4.     <bean id="coder" class="com.ponder.CoderImpl"/>  
  5.     <service id="CoderService" ref="coder" interface="com.ponder.ICoder"/>  
  6.     <!--Embeded bean的osgi服务注册 -->  
  7.     <service id="CoderService2" interface="com.ponder.ICoder">  
  8.         <bean class="com.ponder.CoderImpl"/>  
  9.     </service>  
  10.     <!--带osgi服务属性的服务-->  
  11.     <service id="CoderService3" interface="com.ponder.ICoder">  
  12.         <service-properties>  
  13.             <entry key="param01" value="val01"/>  
  14.             <entry key="param02" value="val02"/>  
  15.             <entry key="param03">  
  16.             <array value-type="java.lang.String">  
  17.             <value>val03-1</value>  
  18.             <value>val03-2</value>  
  19.             <value>val03-3</value>  
  20.             </array>  
  21.         </entry>  
  22.         </service-properties>  
  23.         <bean class="com.ponder.CoderImpl"/>  
  24.     </service>  
  25.     <!-- 没将接口抽离出来的osgi服务,会将com.ponder.CoderImpl里的所有public方法都作为服务的方法 -->  
  26.     <service id="CoderService3"  interface="com.ponder.CoderImpl">  
  27.         <bean class="com.ponder.CoderImpl"/>  
  28.     </service>  
  29. </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节点来引用用这些服务。 

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.     <!--引用osgi服务,并注入bean(com.ponder.Processor)里 -->  
  4.     <reference id="coderService" filter=”(param01=val02)” interface="com.ponder.ICoder" timeout="0"/>  
  5.     <bean id="processor" class="com.ponder.Processor">  
  6.         <!--与这里对应,类com.ponder.Processor里应定义有以下属性:  
  7.         private com.ponder.ICoder coder;  
  8.         并包含其setter。  
  9.         -->  
  10.         <property name="coder" ref="coderService"/>  
  11.     </bean>  
  12.       
  13. </blueprint>  



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

 

本文出自 “色声香味触法” 博客,请务必保留此出处http://killko.blog.51cto.com/3419292/1131103

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值