gpt4 book ai didi

java - 如何将基本的 GWT 组件与 GIN 绑定(bind)?

转载 作者:行者123 更新时间:2023-12-04 21:29:43 25 4
gpt4 key购买 nike

我关注了 basic setup instructions在 GWT-GIN 教程页面上。我在第 3 步(声明绑定(bind))并试图找出如何使用 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!
}
}

上面的四个用例是我正在努力解决的问题。分别是:

  1. 如何在每次客户端请求时重用相同的 EventBus 实例?
  2. 从上面的 #1 构建,如何在不同的场景下拥有 2+ 个不同的实例?
  3. 如何注入(inject)任何内容的 List
  4. 可能与上面的#2 相同,但是如何将 2+ 个 Place 子类绑定(bind)到 Place.class

提前致谢!

最佳答案

很好的问题有助于阐明 Guice 本身的工作原理,以及 Guice 和 Gin 之间的区别。 Gin 与 Guice 不太一样 - configure()方法在生成 JavaScript 时运行,以便编译器仅在正确的类型集中进行烘焙 - 否则您的应用程序可能包含整个 JRE!这对 Gin 来说有点作弊,一旦你理解了这一点,GWT DI 就更有意义了。

基本思想是 configure()方法只应该处理布线 - 而不是创建实例。这提供了 1) 的答案和 2) 的部分答案。实际上编写将在应用程序运行时使用的代码(Provider 对象、@Provides 方法,当然还有任何用 @Inject 注释的代码)需要反过来——它只会被编译成 JS。这意味着虽然您可以定义类似 configureBuzzes 的方法在 3) 中,你只需要小心从 configure() 中调用它们方法 - 并且从不调用configure()来自常规应用程序代码。

2)、3) 和 4) 的答案主要与 Guice 本身的一般工作方式有关。我为 1) 提供的解决方案也适用于普通的 Guice,我会一直建议这种方法 - 我发现如果不混合布线和实际对象构建,它往往会产生更具可读性的代码.

  1. 不要在您的 configure() 中创建实例方法,只需进行绑定(bind)。您可以将绑定(bind)设置为例如

    bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

    创建实例,并将其限定为单例 - 默认情况下将使用默认构造函数。

    • 如果您想使用非默认构造函数,有多种选择。您可以使用 @Inject 注释特定的构造函数,并为每个值提供一些注释(稍后会详细介绍),或者您可以构建提供程序或 @Provides创建实例的方法。同样,您可能需要 @Singleton使这有意义,但这取决于您的用例(这将是您的 GinModule 中的另一种方法):

      @Provides
      //@Singleton //optional, depends on your use case
      public Fizz provideFirstFizz() {
      return new Fizz(true, "oh yeah", null);
      }
    • 接下来,如何提供两种不同类型的同一事物?你如何在 Guice 中做到这一点?您如何期望您的代码获得 Fizz注入(inject)得到正确的?事实证明,这些可能都有相同的答案——你需要找到一种方法来指示你想要哪个实例。它们都是相同的类型,所以这还不够,但我们可以提供其他提示,比如注入(inject)字段上的注释。说出我们的代码需要 f1f2看起来像这样

      @Inject
      @Red// Make up your own annotation, or use some existing ones
      private Fizz f1;

      @Inject @Blue private f2;

      现在我们有办法区分它们,我们需要使用相同的注释来绑定(bind)它们。因为我们仍然假设没有 @InjectFizz 上构造函数,我们不能只做一个 bind()调用,所以我们只添加 @Blue提供方法:

      @Provides
      @Blue
      //@Singleton //optional, depends on your use case
      public Fizz provideFirstFizz() {
      return new Fizz(true, "oh yeah", null);
      }

      我们可以将其解读为“此方法 Provides Blue Fizz 实例”。对于 @Red ,因为我们有默认的构造函数,我们可以使用bind() :

      bind(Fizz.class).annotatedWith(Red.class);//... no need to specify type
      //in this case, but might want
      //singleton

      参见 https://code.google.com/p/google-guice/wiki/BindingAnnotations有关这方面的更多详细信息。

  2. 同样,我们可以使用 @Provides为此,或创建并绑定(bind)一个 Provider<T>类型。由于我们已经完成了几种提供程序方法,让我们尝试 Provider<List<Buzz>> :

    public class BuzzListProvider implements Provider<List<Buzz>> {
    public List<Buzz> get() {
    List<Buzz> buzzes = new ArrayList<Buzz>();
    // Configure them... This might call on a @Inject defined
    // within this BuzzListProvider, on the ctor or a field, or
    // just some code in this method.
    return buzzes;
    }
    }

    然后,将 Provider 绑定(bind)到该列表:

    // cant say List<Buzz>.class, use TypeLiteral instead
    bind(new TypeLiteral<List<Buzz>>(){})
    .toProvider(BuzzListProvider.class);
    // .in(Singleton.class); if the list needs to be only created once
  3. 您的总结完全正确 - 这与 2 完全相同。我通常会做一个 @DefaultPlace注释(或者只是简单的 @Default,这样我就可以重新使用它)来处理这种情况。

关于java - 如何将基本的 GWT 组件与 GIN 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com