【问题标题】:Gin not finding @Provides method杜松子酒找不到@Provides 方法
【发布时间】:2012-02-19 21:24:14
【问题描述】:

这是我的 Gin 模块:

public class FooGinModule extends AbstractGinModule {

  @Override
  protected void configure() {
    ...

    bind(ActivityManager.class).asEagerSingleton();

    ...
  }

  @Provides
  @Singleton
  public ActivityManager getActivityManager(EventBus eventBus, SimplePanel display,
      ActivityMapper activityMapper) {
    final ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
    activityManager.setDisplay(display);
    return activityManager;
  }

}

当我尝试 gwt-compile 时,我收到以下错误:

[错误] 找不到类的 @Inject 或默认构造函数 com.google.gwt.activity.shared.ActivityManager

我指定 @Provides 方法是否错误?

【问题讨论】:

    标签: gwt dependency-injection gwt-gin


    【解决方案1】:

    您不需要显式绑定,实际上我认为它会覆盖@Provides 方法。我不知道将asEagerSingleton()@Provides 方法一起使用的好方法,而是考虑使用Provider<T> 实现。

    bind(ActivityManager.class)
        .toProvider(MyActivityManagerProvider.class)
        .asEagerSingleton();
    

    查看您正在生成/连接的实际内容,我发现将 @Inject ActivityManager 实例实际添加到 SimplePanel 的父级(或您用作显示的任何内容)并调用 @ 很有用987654329@ 那里代替。至少对我来说,这使得更换或更改显示器变得更容易,因为它是该代码块的一部分,而不是模块的一部分。它还消除了创建渴望单例的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 2022-11-24
      • 2023-02-05
      • 2012-01-12
      相关资源
      最近更新 更多