2013-06-04 34 views
2

我已經定義了一個服務: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) 

我安裝拱勾勾,這些都是結果:

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="[email protected]" 

我缺少什麼?

(字節碼操作是由一個Ant任務做了,順便說一句)

回答

0

無需實現BundleActivator的只是使用:

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

好的,如果我得到這個權利的服務將在@調用驗證方法。當我第一次嘗試訪問它時,我認爲服務會被注入? 所以我不能在BundleActivator的啓動方法中使用它? – Tim