【发布时间】:2016-01-07 08:20:06
【问题描述】:
我正在尝试在 Apache karaf 版本 2.3.10 中实现自定义命令。
@Component
@Command (scope = "test", name = "list", description = "list all the test commands")
public class CustomCommand extends OsgiCommandSupport
{
...
@Reference (bind = "bindMethod", unbind = "unbindMethod", cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.DYNAMIC)
private SampleService mySampleService;
...
protected void bindMethod(SampleService aSampleService)
{
mySampleService = aSampleService;
}
protected void unbindMethod(Services aSampleService)
{
mySampleService = null;
}
@Override
protected Object doExecute() throws Exception
{
mySampleService.printCommands(); // nullpointer exception is thrown for non static sampleservice reference
System.out.println("Command printed test:")
}
}
安装捆绑包后,我得到了用于非静态 sampleservice 参考的 NPE。 在 bindServices 中,引用已正确解析。如果我将 Sampleservice 更改为静态,那么一切正常。 这背后的原因是什么?
【问题讨论】:
标签: java osgi apache-karaf osgi-bundle