iPOJO -使用注入服务时的空指针



我已经定义了一个服务:

public interface TimeProvider {
     int getCurrentTime();
}

和实现:

@Component
@Provides
@Instantiate
public class FooTimeProvider implements TimeProvider {
    ...
}

在另一个类(实际上是Activator类)中,我这样引用服务:

@Component
@Provides
public class Activator implements BundleActivator {
    @Requires
    private TimeProvider timeProvider;
    ...
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        ...
        timeProvider.getCurrentTime();
    }
    ...
}

得到NullPointerException

at package.Activator.__M_start(Activator.java:60)

我安装了Arch Gogo,结果如下:

ipojo:instances
Instance org.apache.felix.ipojo.IPOJOURLHandler-0 -> valid 
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -> valid 
Instance package.FooTimeProvider-0 -> valid 
ipojo:handlers
Handler org.apache.felix.ipojo:controller (VALID)
Handler org.apache.felix.ipojo:provides (VALID)
Handler org.apache.felix.ipojo:properties (VALID)
Handler org.apache.felix.ipojo:callback (VALID)
Handler org.apache.felix.ipojo:requires (VALID)
Handler org.apache.felix.ipojo:architecture (VALID)
ipojo:instance package.FooTimeProvider-0
instance name="package.FooTimeProvider-0" state="valid" bundle="16" component.type="package.FooTimeProvider"
handler name="org.apache.felix.ipojo:provides" state="valid"
    provides specifications="[anotherPackage.TimeProvider]" state="registered" service.id="44"
        property name="factory.name" value="package.FooTimeProvider"
        property name="instance.name" value="package.FooTimeProvider-0"
handler name="org.apache.felix.ipojo:architecture" state="valid"
object name="package.FooTimeProvider@6c7e1f14"

我错过了什么?

(字节码操作由Ant任务完成,顺便说一下)

不用实现BundleActivator,只需使用:

@Validate
public void start() throws Exception {
    ...
    timeProvider.getCurrentTime();
}

相关内容

  • 没有找到相关文章

最新更新