我使用hk2作爲CDI引擎。 我有2嵌套注射如以下代碼:HK2 InstantiationService嵌套注入
public class Root {
@Inject
Son son;
...
}
public class Son {
@Inject
GrandSon gs; //should i put it here?
...
}
public class GrandSon {
...
}
這些工廠的類:
public class SonFactory implements Factory<Son>{
@Inject
InstantionService is;
@Inject
GrandSon gs; //should i put it here?
public Son provide(){
is.getInstantiationData()
return sonImpl;
}
public dispose(Son instance){
// destroy
}
}
public GrandsonFactory implements Factory <GrandSon>{
@Inject
InstantionService is
public GrandSon provide(){
is.getInstantiationData()
return sonImple;
}
public dispose(GrandSon instance){
// destroy
}
}
我結合兩個工廠爲: bindFactory(SonFactory.class)。爲了(兒子。類)。在(RequestScoped.class) bindFactory(GrandSonFactory.class)。爲了(GrandSon.class)。在(RequestScoped.class)
現在我想只是用InstantionService.getInstantiationData()從獲得描述符數據c在GrandSon類中的父類。特別是我需要回溯到調用Root類來檢查注入者父類。我可以從Son類的factory.provide方法獲取數據,但我無法從grandSon類獲取有效的getInstantiationdata()。 我的代碼有什麼問題?
在上面的代碼中,我沒有看到誰在提供FirstSon?我看到Son和GrandSon的工廠提供商,但不是FirstSon。 – jwells131313
對不起firstson是兒子...我剛纔編輯 – Alex
獲得工廠服務時,InstantiationService可能存在問題。我會寫一個單元測試,並找出 – jwells131313