iPOJO - 使用注入服务时的NullPointer

时间:2013-06-04 13:47:21

标签: 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任务完成,顺便说一句)

1 个答案:

答案 0 :(得分:0)

而不是实现BundleActivator,只需使用:

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