【发布时间】:2011-11-10 23:24:15
【问题描述】:
我找到了 Guice Overriding Binding in Guice 的答案,但不知道如何在 GWT 中为 GIN 做同样的事情。
提前致谢!
【问题讨论】:
-
为什么要覆盖 GinModule ?
-
嗨,我正在考虑在单元测试中用模拟 impl 替换一些绑定。如果支持,我可以通过扩展模块和覆盖绑定来替换它们...
我找到了 Guice Overriding Binding in Guice 的答案,但不知道如何在 GWT 中为 GIN 做同样的事情。
提前致谢!
【问题讨论】:
据我所知,不支持。
回答您的评论:
如果您正在运行“纯”JUnit 测试(不是 GWTTestcases),那么您不使用 GIN,而是使用 Guice,并且在 Guice 中您可以覆盖模块。如果您想重用 GIN 模块,请使用 GinModuleAdapter 包装它们。所以你可以这样做:
static class MyGinModule extends GinModule {
...
}
static class MyGuiceModule extends AbstractModule {
...
}
// And somewhere in your code, here's how you could create the Injector
Module myWrappedGinModule = new GinModuleAdapter(new MyGinModule());
Module myModule = Modules.override(myWrappedGinModule).with(new MyGuiceModule());
Injector injector = Guice.createInjector(myModule);
【讨论】:
在你的界面中使用@ImplementedBy注解。
注解中指定的类将是默认实现。
您可以指定另一个实现,有效地覆盖默认值。
例如:
@ImplementedBy(MyWidgetImpl.class)
public interface MyWidget {
//...
}
public class MyWidgetImpl implements MyWidget {
//...
}
【讨论】: