2016-12-16 28 views
0

我遇到了小批量中依賴注入的問題。Java EE - 依賴注入到小批量中

@Named 
public class SimpleBatchlet extends AbstractBatchlet { 
    @Inject 
    protected StorageService storageService; 

    ... 

    public String process() throws Exception { 
    storageService.doSomething(); // this throws a null pointer exception 
    } 
} 

@Named 
public class LocalFileStorageService implements StorageService { 
    public void doSomething() { 

    } 
} 

我試着在META-INF和WEB-INF中放置beans.xml,並且刪除它,都無濟於事。我也嘗試將bean的範圍更改爲單例等。我通過在使用BatchRuntime啓動作業的方法上使用@Schedule註釋來調用/啓動批處理作業。

我必須錯過簡單的東西,因爲我知道這應該工作。我將要使用的bean的實際範圍可能需要改變,但是我想說的是我不相信bean範圍是一個問題,但是還有一些其他配置問題。

我還應該注意,我只有1個StorageService實現。

+0

從你的問題中不清楚哪些是有效的,哪些沒有。也許你應該修改它,並提供一些示例代碼 –

+0

更新,希望這有助於。 – Walter

回答

0

不清楚是什麼真的是你的問題(NPE上注入CDI豆?),但你的註解Batchlet @Dependent應該解決的問題:

@Named 
@Dependent 
public class SimpleBatchlet extends AbstractBatchlet { 
    @Inject 
    protected StorageService storageService; 
} 

Batchlet需要被@Named@Dependent與CDI的整合。