我可以使用GIN在GWT的常规类中注入对象吗?
例如,我有一个包含类A绑定的GIN模块。在类B中(B不是使用GIN绑定的),我可以简单地使用:我可以使用GIN在GWT的常规类中注入对象吗?,gwt,dependency-injection,guice,gwt-gin,Gwt,Dependency Injection,Guice,Gwt Gin,例如,我有一个包含类A绑定的GIN模块。在类B中(B不是使用GIN绑定的),我可以简单地使用: @Inject private A a; 注入甲级?我在我的项目中进行了尝试,看起来我得到了对象a的空指针。不知道为什么。因为还需要用GIN实例化B类 例如,对于@UiFields,您可以使用(提供true),然后将它们注入构造函数中,如下所示: /*Class B is not binded by GIN*/ public class B { @Inject
@Inject private A a;
注入甲级?我在我的项目中进行了尝试,看起来我得到了对象a的空指针。不知道为什么。因为还需要用GIN实例化B类 例如,对于@UiFields,您可以使用(提供true),然后将它们注入构造函数中,如下所示:
/*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关键字实例化A,那么B实例的myWidget引用也将为null或者,您的Ginject可以有一个void方法,该方法将B作为唯一参数,并将注入其成员。这与Guice的Injector.injectMembers类似(可能应该命名为类似)。