title: How to use two Guice modules that install a common dependency module
body: <p>I'm working on a project that consists of four parts:</p>
  
  <ul>
  <li>The <code>Main</code> project that brings everything together. This contains the <code>public static void main(String... args)</code> entry point.</li>
  <li>Component <code>A</code></li>
  <li>Component <code>B</code></li>
  <li>A 3rd party <code>Common</code> component that both <code>A</code> and <code>B</code> refer to.</li>
  </ul>
  
  <p>I'm using Guice for the plumbing between all four parts, and this is my problem:<br/>
  In both <code>A</code>s and <code>B</code>s main Guice modules I install a module that extends one that is defined in <code>Common</code>. At runtime this setup fails with the following error:</p>
  
  <blockquote>
    <p>A binding to <code>common.SomeClass</code> was already configured at <code>common.AbstractCommonModule.configure()</code>. [<a href="https://github.com/google/guice/blob/d7354eceabdefbf5b1a14bc648786aa8535e4ff6/core/src/com/google/inject/internal/Errors.java#L330" rel="noreferrer">source</a>]</p>
  </blockquote>
  
  <p>The reason for this is that I'm invoking <code>common.AbstractCommonModule.configure()</code> twice; once by installing a subclass instance of <code>common.AbstractCommonPrivateModule</code> from Component <code>A</code>'s <code>com.a.MainModule.configure()</code>, and a second time from Component <code>B</code>'s <code>com.b.MainModule.configure()</code>.</p>
  
  <p>Installing just <em>one</em> instance of <code>common.AbstractCommonPrivateModule</code> in <code>Main</code> is not an option, because <code>AbstractCommonPrivateModule</code> implements a specific binder method <code>bindComplicatedStuff(ComplicatedStuff)</code>, for which I only know the argument inside <code>A</code> and <code>B</code>, respectively.</p>
  
  <p>I tried working around this whole thing by wrapping <code>A</code>'s and <code>B</code>'s respective main Guice modules in <a href="http://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/PrivateModule.html" rel="noreferrer"><code>PrivateModule</code></a>s. However, this failed with the next error:</p>
  
  <blockquote>
    <p>Unable to create binding for %s. It was already configured on one or more child injectors or private modules %s%n  If it was in a PrivateModule, did you forget to expose the binding? [<a href="https://github.com/google/guice/blob/d7354eceabdefbf5b1a14bc648786aa8535e4ff6/core/src/com/google/inject/internal/Errors.java#L346-L351" rel="noreferrer">source</a>]</p>
  </blockquote>
  
  <p>In my case, <code>A</code>'s and <code>B</code>'s respective main Guice modules are in fact <a href="http://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/servlet/ServletModule.html" rel="noreferrer"><code>ServletModule</code></a>s - which apparently I <em>can</em> install twice from <code>Main</code>.</p>
  
  <p><strong>How can I get around these errors and install the <code>AbstractCommonPrivateModule</code> module twice?</strong></p>
  
  <p><strong>Edit:</strong> I uploaded some example code (with explanation about some details) <a href="https://github.com/derabbink/simple_web_stack" rel="noreferrer">to GitHub</a></p>