title: Injecting a composite with Guice
body: <p>Suppose I have an interface <code>Foo</code>.  It is implemented by concrete implementations <code>CompositeFoo</code>, <code>FooA</code>, <code>FooB</code>, and <code>FooC</code>.  Moreover, <code>CompositeFoo</code> is as follows:</p>
  
  <pre><code>public class CompositeFoo implements Foo {
      @Inject public CompositeFoo(List&lt;? extends Foo&gt; elements);
  }
  </code></pre>
  
  <p>I wish to, in a Guice <code>PrivateModule</code>, bind <code>Foo</code> to a <code>CompositeFoo</code> with the list being a <code>FooA</code> followed by something that is either a <code>FooB</code> or a <code>FooC</code>.  (This must be a list, as order matters; this rules out multibindings as a solution.)</p>
  
  <p>The problem is that I am seeing some circularity involved.  Suppose the provider of the <code>CompositeFoo</code> is as follows:</p>
  
  <pre><code>public class CompositeFooProvider implements Provider&lt;Foo&gt; {
      @Inject private FooA first;
      @Inject @Named("Inner") private Foo second;
  
      @Override public Foo get() { return new CompositeFoo(asList(first, second)); }
  }
  </code></pre>
  
  <p>The module providing the second <code>Foo</code> (either <code>FooB</code> or <code>FooC</code>) is as follows:</p>
  
  <pre><code>public class InnerModule extends PrivateModule {
      private final Key&lt;? super Foo&gt; bindingKey;  // key will be exposed, bound to the Foo below
  
      // configure() deals with deps of FooB and FooC
  
      @Provides
      public Foo getInnerFoo(...) {
          // Assume that the args are such that if they are "valid", we should return a FooB, else FooC
          if (...) return new FooB(...);
          else return new FooC(...);
      }
  }
  </code></pre>
  
  <p>The circularity arises when I try to construct the outer module: I need to install the <code>InnerModule</code> (passing in <code>Key.get(Foo.class, Names.named("Inner"))</code> as the binding key) in order to get the second <code>Foo</code>, but <code>Foo</code> is already bound in the outer module due to its binding to the <code>CompositeFooProvider</code>.  How do I resolve this circularity?  Will converting the <code>@Provides</code> method to its own <code>Provider</code> suffice?</p>