我有一个web应用程序使用GIN在入口点注入依赖关系。Guice /杜松子酒。如何注入多个实现
private InjectorService injector = GWT.create(InjectorService.class);
@GinModules({PlaceContollerInject.class, RootViewInject.class})
public interface InjectorService extends Ginjector {
RootView getRootView();
PlaceController getPlaceConroller();
}
public class RootViewInject extends AbstractGinModule {
@Override
protected void configure() {
bind(RootView.class).to(RootViewImpl.class);
}
}
我需要使用不同的RootView实现的移动版本。依赖以下模块
public class RootViewMobileInject extends AbstractGinModule {
@Override
protected void configure() {
bind(RootView.class).to(RootViewMobileImpl.class);
}
}
中描述的问题是如何选择需要依赖我们有条件是否需要移动或默认版本。 我见过GWT-GIN Multiple Implementations,但还没有找到解决方案,因为Provider违反了依赖关系的链条,Factory Pattern破坏了可测试性。 在“具有Guice的大型模块化Java”视频here (12 minute) Guice的模块注入器作为工厂的替代品提供。所以我的问题是我应该为我的应用程序的移动版和默认版本(如MobileFactory和DefaultFactory)创建不同的Ginjector,否则这将是不好的做法,我应该为所有需要的版本配置一个Ginjector实例。例如像这样的注释绑定。
public class RootViewMobileInject extends AbstractGinModule {
@Override
protected void configure() {
bind(RootView.class).annotatedWith(Mobile.class).to(RootViewMobileImpl.class);
}
}
,并使用在GWT入口点
@Mobile注释绑定在这样的简化的例子,因为它上面是可能的。但是如果一个应用程序有更多的需要移动和默认版本的依赖。它看起来回到了无法测试的“丑陋”(正如在Guice的演讲中所说的)工厂。 对不起,我的英文。任何帮助表示赞赏。
我只想补充一点,这里的想法和@aldanok的解决方案也可以用于应用程序中的客户端特定定制。特定于客户端:gwt.xml + gin模块+接口实现。 – MeTTeO