这里是对上一篇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(策略)可选值为:static和dynamic, 静态和动态的意思。如果是静态的话,如果Service重新加载,Component也会重新加载。而dynamic只会调用相应的setService()和unsetService()方法。
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咯!
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。