我需要将一些参数从felix启动器类(Main)传递给bundle,然后我跟着this advice。我做了什么
启动项目
test.launcher包中包含:Main.java,Temp.java,TempI.java
捆绑项目
test.bundle包中包含:Activator.java
test.launcher包中包含:TempI.java
正如您所看到的,我将TempI.java复制到捆绑项目,因为启动器不是捆绑包,并且无法导出其包。
在主要
BundleContext context = felix.getBundleContext();
ServiceRegistration serviceRegistration=context.registerService(TempI.class.getName(), new Temp(), null);
在Activator中
ServiceReference serviceReference = context.getServiceReference(TempI.class.getName());
TempI service = (TempI) context.getService(serviceReference);
System.out.println(service.testService());
但我进入了Activator
java.lang.NullPointerException:指定的服务引用不能 空。
有什么问题?再次不同的类加载器?或者是什么?
答案 0 :(得分:1)
您声明启动器和捆绑包都包含自己的TempI类副本。一个由启动器的类加载器加载,另一个由bundle的类加载器加载。对于VM以及OSGi服务注册表,这些是不同的Class对象。因此,当您查找捆绑包中已知的TempI类的TempI服务时,它会找不到。
您不需要将TempI放入捆绑包中,而是导入包。您还需要配置框架以使用org.osgi.framework.system.packages.extra导出包。然后,bundle将从系统包导入包,并且该包中将显示在启动器中注册的服务。