bundleContext.getServiceReference()获取服务为null

血饮 2014-07-18 10:22:14
本人刚接触OSGI,对里面的以下原理及流程还不是很清楚,现在遇到这样一个问题:

在eclipse创建了一个bundleA,并在该bundleA的Activator中注册了一个服务,注册代码如下

public class Activator implements BundleActivator {

private BundleContext _context;
private A a;
public void start(BundleContext context) throws Exception {

this._context = context;
registerBundleService();
getBundleService();
}

/**
* 获取服务
*/
private void getBundleService() {
ServiceReference reference = _context.getServiceReference (A.class.getName());//服务引用
if(null!=reference){
this.a=(A)_context.getService(reference);
}

}

/**
* 注册服务
*/
private void registerBundleService() {
_context.registerService(A.class.getName(),
new A(), null);
}

public void stop(BundleContext context) throws Exception {
context = null;
this.nativeService=null;

}

}

然后将此bundleA放到osgi felix下运行没问题,但当我另外创建一个bundleB,想通过bundleB的Activator 引用服务,结果_context.getServiceReference (A.class.getName());获取不到bundleA注册的服务,请问是什么原因!
...全文
337 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hulei313649798 2015-07-24
  • 打赏
  • 举报
回复
同样遇到了。。不知道你解决了没

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧