Guice:可以注入模块吗?

sxc*_*731 25 java dependency-injection guice

我有一个需要一些的模块Depedency.是否可以注入模块本身?我意识到这有点鸡蛋和鸡蛋的情况......

例:

public class MyModule implements Module {

    private final Dependency d_;

    @Inject public MyModule(Dependency d) {
        d_ = d;
    }

    public void configure(Binder b) { }

    @Provides Something provideSomething() {
        // this requires d_
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在这种情况下,解决方案是将@Provides方法转变为一个完整的Provider<Something>类.这显然是一个简化的例子; 我正在处理的代码有很多这样的@Provides方法,所以将它们分成各个Provider<...>类并引入一个模块来配置它们会增加相当多的混乱 - 我认为Guice是关于减少样板杂乱的一切?

也许这反映了我对Guice的相对苛刻,但我遇到了一些我很想做上述事情的案例.我肯定错过了什么...

Col*_*inD 29

@Provides方法可以将依赖项作为参数,就像带@Inject注释的构造函数或方法的参数一样:

@Provides Something provideSomething(Dependency d) {
   return new Something(d); // or whatever
}
Run Code Online (Sandbox Code Playgroud)

这是记录在这里,虽然也许它可以作出更加突出.


Jim*_*rne 12

如果需要依赖项来手动构造对象,则使用提供程序或@Provides方法非常有用.但是,如果您需要某些东西来帮助您决定如何配置绑定本身呢?事实证明,您可以使用Guice来创建(和配置)您的模块.

这是一个(人为的)例子.首先,我们要配置的模块:

/**
 * Creates a binding for a Set<String> which represents the food in a pantry.
 */
public class PantryModule extends AbstractModule {
  private final boolean addCheese;

  @Inject
  public ConditionalModule(@Named("addCheese") boolean addCheese) {
    this.addCheese = addCheese;
  }

  @Override
  protected void configure() {
    Multibinder<String> pantryBinder = Multibinder
      .newSetBinder(binder(), String.class);

    pantryBinder.addBinding().toInstance("milk");

    if (addCheese) {
      pantryBinder.addBinding().toInstance("cheese");
    }

    pantryBinder.addBinding().toInstance("bread");
  }
}
Run Code Online (Sandbox Code Playgroud)

PantryModule需要注入一个布尔值来决定它是否应该在食品室中包含奶酪.

接下来,我们将使用Guice配置模块:

// Here we use an anonymous class as the "configuring" module. In real life, you would 
// probably use a standalone module.
Injector injector = Guice.createInjector(new AbstractModule() {
  @Override
  protected void configure() {
    // No cheese please!
    bindConstant().annotatedWith(Names.named("addCheese")).to(false);
    bind(PantryModule.class);
  }
});

Module configuredConditionalModule = injector.getInstance(PantryModule.class);
Run Code Online (Sandbox Code Playgroud)

现在我们已经配置了模块,我们将更新我们的注入器以使用它...

//...continued from last snippet...
injector = injector.createChildInjector(configuredConditionalModule);
Run Code Online (Sandbox Code Playgroud)

最后我们将获得代表我们食品室的字符串集:

//...continued from last snippet...
Set<String> pantry = injector.getInstance(new Key<Set<String>>() {});

for (String food : pantry) {
  System.out.println(food);
}
Run Code Online (Sandbox Code Playgroud)

如果您将所有部分放在main方法中并运行它,您将获得以下输出:

milk
bread
Run Code Online (Sandbox Code Playgroud)

如果将绑定更改为"addCheese"布尔值为true,您将获得:

milk
cheese
bread
Run Code Online (Sandbox Code Playgroud)

这种技术很酷,但可能仅在您控制Injector实例并且仅在模块需要复杂依赖项时才有用.毫无道理,我发现在一个真正的工作项目中真正需要这个.如果我这样做,那么其他人也可能.


CJC*_*CJC 5

这个问题已经得到了很好的回答,但我只是想为Colin的例子添加一个变体:

class MyModule extends AbstractModule { 
  public void configure() {
    bind(Something.class).toProvider(new Provider<Something>() {
       @Inject Dependency d;
       Something get() { return d.buildSomething(); }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

该@Provides方法的方法是不是我有什么上面这个简单的例子清晰,但我发现,实例化一个实际的提供者可以在某些情况下是有用的.我从邮件列表中偷走了一些东西; 不会发生在我自己身上;)