Java GWT/Gin演示者实例化
我已经按照的建议实现了一个GWT应用程序。我还下载了示例代码,我注意到演示的每个演示者都需要注入到主演示者构造函数中进行实例化。作者在资料来源中包括了这一点:Java GWT/Gin演示者实例化,java,gwt,gwt-gin,gwt-mvp,Java,Gwt,Gwt Gin,Gwt Mvp,我已经按照的建议实现了一个GWT应用程序。我还下载了示例代码,我注意到演示的每个演示者都需要注入到主演示者构造函数中进行实例化。作者在资料来源中包括了这一点: public class GreetingPresenter extends WidgetPresenter<GreetingPresenter.Display> { // FUDGE FACTOR! Although this is not used, having GIN pass the object
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();
}
ginject:
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();
}
我认为您缺少Ginject和AbstractGinModule定义,您在其中定义了该示例中的GIN绑定。
我建议从上述教程中下载,并查看GIN入门 问候接受者:
@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
public interface GreetingGinjector extends Ginjector {
AppPresenter getAppPresenter();
PlaceManager getPlaceManager();
}
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);
}
}
public class GreetMvp implements EntryPoint {
private final GreetingGinjector injector = GWT.create(GreetingGinjector.class);
public void onModuleLoad() {
final AppPresenter appPresenter = injector.getAppPresenter();
appPresenter.go(RootPanel.get());
injector.getPlaceManager().fireCurrentPlace();
}
}
问候客户模块:
@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
public interface GreetingGinjector extends Ginjector {
AppPresenter getAppPresenter();
PlaceManager getPlaceManager();
}
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);
}
}
public class GreetMvp implements EntryPoint {
private final GreetingGinjector injector = GWT.create(GreetingGinjector.class);
public void onModuleLoad() {
final AppPresenter appPresenter = injector.getAppPresenter();
appPresenter.go(RootPanel.get());
injector.getPlaceManager().fireCurrentPlace();
}
}
入口点:
@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
public interface GreetingGinjector extends Ginjector {
AppPresenter getAppPresenter();
PlaceManager getPlaceManager();
}
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);
}
}
public class GreetMvp implements EntryPoint {
private final GreetingGinjector injector = GWT.create(GreetingGinjector.class);
public void onModuleLoad() {
final AppPresenter appPresenter = injector.getAppPresenter();
appPresenter.go(RootPanel.get());
injector.getPlaceManager().fireCurrentPlace();
}
}
您可以通过使用提供者来解决这个问题。在我的Gin模块中,我声明了我的演示者/活动及其视图,如下所示
// Content Area
bind(ContentActivityMapper.class).in(Singleton.class);
// Intro Page
bind(IntroPageActivity.class);
bind(IntroPageView.class).to(IntroPageViewImpl.class).in(Singleton.class);
public class ContentActivityMapper implements ActivityMapper {
@Inject Provider<IntroPageActivity> introPageProvider;
public Activity getActivity(Place place) {
if (place instanceof DefaultPlace) {
return introPageProvider.get().withPlace(new IntroPagePlace());
}...
然后在活动映射器中,我使用如下提供程序
// Content Area
bind(ContentActivityMapper.class).in(Singleton.class);
// Intro Page
bind(IntroPageActivity.class);
bind(IntroPageView.class).to(IntroPageViewImpl.class).in(Singleton.class);
public class ContentActivityMapper implements ActivityMapper {
@Inject Provider<IntroPageActivity> introPageProvider;
public Activity getActivity(Place place) {
if (place instanceof DefaultPlace) {
return introPageProvider.get().withPlace(new IntroPagePlace());
}...
我下载了源代码并进行了测试,其中包括您提到的类。尽管如此,GreetingResponsePresenter仍然需要“捏造因素”:所使用的MVP框架已经具备了演示者/展示注入的功能(参见另一个答案)。但是,我们仍然需要每个演示者作为应用程序工作的主要演示者的参数:SI在示例中没有显示他们,但我在这个应用程序中有大约20个不同的活动/演示者。我不必将它们嵌入maim演示器中。这就是我所需要的,它们都是根据需要创建的,甚至可以与AsyncProvider类一起用于代码拆分。我正在尝试使用GWTP来实现这一点,但我的提供程序始终未定义。。父演示者声明了
@Injected Provider myAbc
,但似乎缺少一些内容。知道问题是什么吗?我没有足够的信息来帮助你。我唯一的猜测是Gin模块文件中没有绑定某些内容。