DS的reference属性解析

 这里是对上一篇blog: http://winseclone.iteye.com/blog/1786637 的补充。说明DS声明服务XML中reference节点属性的作用。

 

这里reference的几个属性cardinality和policy是比较难理解的。

    cardinality(基数),该属性是DS为啥这么牛逼的真正所在之一。该属性控制Service数量的上下限,达到这个下限后该Component才可用。

    这里说一下“上限”,假设选的是0..1或1..1,就算你的运行时注册了n个Service,框架也只会选择其中一个!至于真正使用那个,不是很确定!所以最好不要这样用!

  • 0..1: optional and singular, "zero or one"
  • 1..1: mandatory and singular, "exactly one"
  • 0..n: optional and multiple, "zero to many"
  • 1..n: mandatory and multiple, "one to many" or "at least one"

上限是1的情况下,找到一个Service后就被break了!所以,在这种“基数”的配置下,不推荐注册多个Service。如果需要运行时替换服务,首先stop原来的服务插件,然后再start新服务的插件。

在0..1或1..1的策略下,也不推荐在Component(Helper)的插件同时绑定一个Service实现,不利于服务的替代!

 


 

 

    policy(策略)可选值为:staticdynamic, 静态和动态的意思。如果是静态的话,如果Service重新加载,Component也会重新加载。而dynamic只会调用相应的setService()和unsetService()方法。

 

动态dynamic策略 写道
osgi> register user-service : sample.http.ds.user.cn.UserServiceImpl@19e5e6b
activate sample.http.ds.UserHelper@48cbd6

osgi> ls
All Components:
ID State Component Name Located in bundle
1 Active sample.http.ds.UserHelper sample.http(bid=41)
2 Active sample.http.en.userService sample.http.ds.user.en(bid=44)
3 Active sample.http.cn.userService sample.http.ds.user.cn(bid=45)

osgi> stop 45
register user-service : sample.http.ds.user.en.UserServiceImpl@406c4
unregister user-service : sample.http.ds.user.cn.UserServiceImpl@19e5e6b

 注意: 这里的顺序!先register后再unregister!所以原来的UserHelper需要进行修改才行,不然,动态变更后,service就变成null咯!

 

静态static策略 写道
osgi> register user-service : sample.http.ds.user.cn.UserServiceImpl@18f07f1
activate sample.http.ds.UserHelper@1526a45

osgi> ls
All Components:
ID State Component Name Located in bundle
1 Active sample.http.ds.UserHelper sample.http(bid=41)
2 Active sample.http.en.userService sample.http.ds.user.en(bid=44)
3 Active sample.http.cn.userService sample.http.ds.user.cn(bid=45)

osgi> stop 45
deactivate sample.http.ds.UserHelper@1526a45
unregister user-service : sample.http.ds.user.cn.UserServiceImpl@18f07f1
register user-service : sample.http.ds.user.en.UserServiceImpl@5c8c05
activate sample.http.ds.UserHelper@1ecade2

   

从上面两个从Console命令窗口中的可以看到,dynamic策略在Service注册后,不会deactivate钝化Component。而static策略会先钝化Component后在unregsiter注销Service。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值