2016-01-13 77 views

回答

2

因爲你需要用GIN實例化你的B類。

例如你可以使用(提供真實的),然後注入其在構造函​​數中,這樣@UiFields:

/*Class B is not binded by GIN*/ 
    public class B { 
     @Inject 
     C cInstance; //C is binded by GIN 
    } 

/*Class A is binded with GIN*/ 
    public class A extends ViewImpl{ 

       @UiField(provided=true) 
       B myWidget; 

       //and then 

       @Inject 
       public A (UiBinder binder, B myWidget){ 
        this.myWidget = myWidget; //cInstance inside of it is injected! 
       } 
     } 

這種B的注射後,所有的B內的@Inject註解應按預期解決。

而如果你實例化一個與GWT.create/new關鍵字 - 對於B例如進myWidget參考值將是空也

+1

或者,您可以Ginjector有需要B作爲唯一的參數無效的方法,並且將注入其成員。這表現得像Guice的Injector.injectMembers(可能應該被命名爲)。 –

相關問題