9

この使用例は、Guice のロボット脚の例と非常によく似ていますが、「脚」の数がわからない点が異なります。したがって、ロボットの脚の例に必要な注釈を使用できません。

Guice の Multibindings 拡張機能を使用して、これらすべての「足」を java.util.Set に集めることを期待しています。

技術的には、PrivateModule では、Multibindings 拡張によって提供されるセットの要素として実装を直接公開したいと考えています。私はそれを行う方法がわかりません。

リファレンスとコード例については、ロボット脚の例を参照してください: http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_do_I_build_two_similar_but_slightly_different_trees_of_objec


これが私の正確なユースケースです:

私は次のものを持っています:

// Main application
public interface MyTree {...}
public interface MyInterface {
  public MyTree getMyTree() {}
}
public abstract class MyModule extends PrivateModule {}
public class MyManager {
  @Inject MyManager (Set<MyInterface> interfaces){ this.interfaces = interfaces }
}
public class MainModule extends AbstractModule {
  public void configure () {
    // Install all MyModules using java.util.ServiceLoader.
  }
}


// In expansion "square.jar"
public class SquareTree implements MyTree {...}
public class SquareImplementation implements MyInterface {
  @Inject SquareImplementation (MyTree tree) { this.tree = tree; }
  public MyTree getMyTree () { return this.tree; }
}
public class SquareModule extends MyModule { // correctly defined as a ServiceLoader's service.
  public void configure () {
    // How to make this public IN a multibinder's set?
    bind(MyInterface.class).to(SquareImplementation.class);

    // Implementation specific to the Squareimplementation.
    bind(MyTree.class).to(SquareTree.class);
  }
}

// In expansion "circle.jar"
public class CircleTree implements MyTree {...}
public class CircleImplementation implements MyInterface {
  @Inject CircleImplementation (MyTree tree) { this.tree = tree; }
  public MyTree getMyTree () { return this.tree; }
}
public class CircleModule extends MyModule { // correctly defined as a ServiceLoader's service.
  public void configure () {
    // How to make this public IN a multibinder's set?
    bind(MyInterface.class).to(CircleImplementation.class);

    // Implementation specific to the Circle implementation.
    bind(MyTree.class).to(CircleTree.class);
  }
}

私は拡張 jar について話しているので、最初はそれらを知りません。それらがいくつ存在するかさえ知りませMyModuleん。わかった)。j.u.ServiceLoaderMyInterface

問題は、すべてのMyInterface実装を 1 つのセット ( MyManager) にまとめることです。どうやってやるの?


完全にジェシーの答えに基づいた解決策:

// Create the set binder.
Multibinder<MyInterface> interfaceBinder = Multibinder.newSetBinder(binder(), MyInterface.class, MyBinding.class);

// Load each module that is defined as a service.
for (final MyModule module : ServiceLoader.load(MyModule.class)) {

  // Generate a key with a unique random name, so it doesn't interfere with other bindings.
  final Key<MyInterface> myKey = Key.get(MyInterface.class, Names.named(UUID.randomUUID().toString()));
  install(new PrivateModule() {
    @Override protected void configure() {
      // Install the module as part of a PrivateModule so they have full hands on their own implementation.
      install(module);
      // Bind the unique named key to the binding of MyInterface.
      bind(myKey).to(MyInterface.class);
      // Expose the unique named binding
      expose(myKey);
    }
  });
  // bind the unique named binding to the set
  interfaceBinder.addBinding().to(myKey);
}

これにより、「顧客」に PrivateModule の拡張を強制するのではなく、MyModule が Module を拡張するインターフェースである場合は、任意のモジュール実装を使用できます。

4

1 に答える 1

10

トップレベルのインジェクターのマルチバインディングに入れることができるように、プライベートモジュールからバインディングをプロミットするためにいくつかのフープをジャンプする必要があるようです。

これはうまくいくはずです:

public class SquareModule extends AbstractModule { // does not extend PrivateModule
  @Overide public void configure() {
    // this key is unique; each module needs its own!
    final Key<MyInterface> keyToExpose
        = Key.get(MyInterface.class, Names.named("square"));

    install(new PrivateModule() {
      @Override public void configure() {

        // Your private bindings go here, including the binding for MyInterface.
        // You can install other modules here as well!
        ...

        // expose the MyInterface binding with the unique key
        bind(keyToExpose).to(MyInterface.class);
        expose(keyToExpose);
      }
    });

    // add the exposed unique key to the multibinding
    Multibinder.newSetBinder(binder(), MyInterface.class).addBinding().to(keyToExpose);
  }
}

マルチバインディングはトップレベルのインジェクターで発生する必要があるため、この回避策が必要です。ただし、プライベート モジュール バインディングはそのインジェクターには表示されないため、公開する必要があります。

于 2011-07-08T15:22:47.743 に答える