📄 osgi框架下servicefactory的实现.txt
字号:
OSGi框架下ServiceFactory的实现2007-05-15 09:10以前写的程序直接就是通过向框架注册服务实现,今天心血来潮,想用OSGi提供的ServiceFactory来改写一下。刚开始肯定是困难重重,因为在网上几乎没搜到相关的文章,并且规范也说的比较模糊。不过在李铭哥的帮助下,还是很快完成了helloworld的改写。现在把改写的部分贴出来,供大家学习。
1.在实现的类中需要实现ServiceFactory接口
public class subhelloprint implements helloprint,ServiceFactory
{
public void welcome(String s)
{
System.out.println("welcome,"+s+"!");
}
public helloprint getService(Bundle bundle, ServiceRegistration reg) {
return new subhelloprint();
}//可以根据不同的Bundle返回不同的服务
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
}
}
2.在服务注册的激活器中将start改成,
public void start(BundleContext context) throws Exception {
reg = context.registerService(helloprint.class.getName(), new subhelloprint(), null);
}
3.引用的过程与Servic注册实现的一样
总结:就像规范里边提到的那样,让framework为每个想使用该服务的bundle获得独享的服务对象。在返回的时候可以根据不同的calling bundle返回不同的service,达到定制服务的目的。至于进一步的应用还是得需要继续学习。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -