【发布时间】:2012-11-16 00:11:12
【问题描述】:
我已关注 GWT-GIN 教程页面上的 basic setup instructions。我在第 3 步(Declare bindings)并试图弄清楚如何使用 GIN 的 Binder API。
public class MyModule extends AbstractGinModule {
@Override
public void configure() {
// 1. Declare an instance of EventBus and make sure every
// injection request pulls back the same instance.
EventBus eventBus = new EventBus();
bind(EventBus.class).to??? // no toInstance() method!
// 2. Declare two instances of Fizz using different constructors,
// and allow the injection of either of them.
Fizz f1 = new Fizz(true, "oh yeah", null);
Fizz f2 = new Fizz();
bind(Fizz.class).to??? // no toInstance() AND don't know how to choose f1 or f2!
// 3. Declare a List of Buzz objects and allow them to be
// injectable.
List<Buzz> buzzes = new ArrayList<Buzz>();
configureBuzzes(buzzes); // adds configured Buzz objects to the list
bind(???).to(buzzes); // no toInstance() methods AND how to bind List<?>.class?!
// 4. Conditionally bind SomePlace to Place *only* when we want the default Place
// that 'historyHandler.handleCurrentHistory()' will go to when called onModuleLoad.
bind(Place.class).to(SomePlace.class); // forces me to only have SomePlace instances!
}
}
上面的四个用例是我正在努力解决的问题。分别:
- 如何在每次客户端请求时重用
EventBus的同一个实例? - 从上面的 #1 构建,如何在不同的场景下有 2+ 个不同的实例可以注入?
- 如何注入
List的任何东西? - 可能和上面的#2一样,但是如何将2+ Place子类绑定到
Place.class?
提前致谢!
【问题讨论】:
标签: java gwt dependency-injection gwt-gin