我按照本教程的建议实现了一个 GWT 应用程序。我还下载了示例代码,我注意到演示的每个 Presenter 都需要注入到主 Presenter 构造函数中才能被实例化。作者在源代码中包含了这一点:
public class GreetingPresenter extends WidgetPresenter<GreetingPresenter.Display> {
// FUDGE FACTOR! Although this is not used, having GIN pass the object
// to this class will force its instantiation and therefore will make the
// response presenter listen for events (via bind()). This is not a very good way to
// achieve this, but I wanted to put something together quickly - sorry!
private final GreetingResponsePresenter greetingResponsePresenter;
@Inject
public GreetingPresenter(final Display display, final EventBus eventBus, final DispatchAsync dispatcher, final GreetingResponsePresenter greetingResponsePresenter) {
super(display, eventBus);
this.dispatcher = dispatcher;
this.greetingResponsePresenter = greetingResponsePresenter;
bind();
}
我验证了如果没有在 GreetingPresenter 中注入任何 Presenter,它将不会被实例化。出于演示目的,这很酷,因为整个应用程序只有两个 Presenter,但在真实世界的应用程序中,这可能会带来严重的不便。
完成 Presenter 实例化的正确方法是什么?
编辑:包括 GIN 相关类以供参考:
演讲者模块:
public class GreetingClientModule extends AbstractPresenterModule {
@Override
protected void configure() {
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
bind(PlaceManager.class).in(Singleton.class);
bindPresenter(GreetingPresenter.class, GreetingPresenter.Display.class, GreetingView.class);
bindPresenter(GreetingResponsePresenter.class, GreetingResponsePresenter.Display.class, GreetingResponseView.class);
bind(AppPresenter.class).in(Singleton.class);
bind(CachingDispatchAsync.class);
}
}
注射器:
@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
public interface GreetingGinjector extends Ginjector {
AppPresenter getAppPresenter();
PlaceManager getPlaceManager();
}