Mic*_*gue 3 gwt gwt-gin dependency-injection inject guice
在变量上使用Inject我有点失落.
我得到了这段代码:
private XXServiceAsync xxServiceAsync;
@Inject
protected IndexViewImpl(EventBus eventBus, XXServiceAsync tableManagementServiceAsync) {
super(eventBus, mapper);
this.xxServiceAsync = xxServiceAsync;
initializeWidgets();
}
Run Code Online (Sandbox Code Playgroud)
使用这段代码,我可以在类中的任何地方调用我的RPC服务(点击...)我想通过直接注入变量来清除一些代码; 这样做 :
@Inject
private XXServiceAsync xxServiceAsync;
protected IndexViewImpl(EventBus eventBus) {
super(eventBus, mapper);
initializeWidgets();
}
Run Code Online (Sandbox Code Playgroud)
这始终使Service保持为NULL.难道我做错了什么 ?带有rpc服务的GIN魔法是否意味着要做其他事情?
谢谢!
那时它仍然是null,因为Gin(和Guice,以及像这样的其他框架)在构造函数完成运行之前无法分配字段.
考虑如果您手动连接代码,这会是什么样子(请记住,Gin/Guice会欺骗一些来分配私有字段,调用不可见的方法):
MyObject obj = new MyObject();//initializeWidgets() runs, too early!
obj.xxServiceAsync = GWT.create(xxService.class);
Run Code Online (Sandbox Code Playgroud)
如果您需要构造函数中的某些内容,请将其传递给构造函数.如果您不需要立即(例如直到调用asWidget()),那么使用@Inject注释的字段或setter可能会有所帮助.