title: Instantiating per-scope/group singletons - stuck in Guice dependency hell
body: <p>This question is a continuation of <strong><a href="https://stackoverflow.com/questions/13708918/using-guice-for-a-framework-with-injected-classes-proper-way-to-initialize">Using guice for a framework with injected classes, proper way to initialize?</a></strong>, which I've tried to implement, and also tried other ways to get around the issue, but nothing has worked so far.</p>
  
  <p>The main issue is this. I have an <code>InterfaceA</code> and <code>InterfaceB</code> that are exposed in different parts of the API. There are two classes that implement both of these interfaces, <code>TestClass</code> and <code>RealClass</code>, so that depending on whether I'm testing or doing something else, I can do the following:</p>
  
  <pre><code>bind(InterfaceA.class).to(TestClass.class);
  bind(InterfaceB.class).to(TestClass.class); 
  </code></pre>
  
  <p>or, for production:</p>
  
  <pre><code>bind(InterfaceA.class).to(RealClass.class);
  bind(InterfaceB.class).to(RealClass.class);
  </code></pre>
  
  <p>I have two requirements for using these classes:</p>
  
  <ol>
  <li>I need the same instance of <code>TestClass</code> or <code>RealClass</code> to be bound to all injections of <code>InterfaceA</code> and <code>InterfaceB</code>; so, like a singleton pattern, except that:</li>
  <li>The singleton is only for a specific scope or child injector, many of which are created during the execution of the program.</li>
  </ol>
  
  <p>The default no-scope approach causes multiple instances of <code>RealClass</code>/<code>TestClass</code> to be created for each interface injection. I don't want that, so I've tried implementing this with scopes, child injectors, and other methods. Nothing has worked:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9942782/custom-guice-scope-or-a-better-approach">Child injector approach</a>: I create a new injector and try to bind the <code>TestClass</code> or <code>RealClass</code> to a singleton instance in that injector. The problem is, whether <code>TestClass</code> or <code>RealClass</code> is being used is configured in the parent injector, and <a href="https://stackoverflow.com/questions/8053515/guice-eager-lazy-singleton-instantiations">since it's a singleton, it's already instantiated</a> (unless in <code>Stage.DEVELOPMENT</code>). There's no way to bind <code>InterfaceA</code> to <code>TestClass</code>, in the parent injector, for example, and then re-bind it as a singleton in the child injector.</li>
  <li>Scope approach: <a href="https://stackoverflow.com/questions/13708918/using-guice-for-a-framework-with-injected-classes-proper-way-to-initialize">I create a custom scope</a> and annotate <code>TestClass</code> and <code>RealClass</code>. Then, I enter and exit this scope to get single instances in that scope. The problem is that my code is multithreaded and having the scope change from one thread affects what the global injector can see and mucks up creating other instances.</li>
  <li><p>Combined child injector and scope approach. I tried creating a child injector for each use of this custom scope, but then binding <code>RealClass</code> in the parent fails with </p>
  
  <pre><code>No scope is bound to name.package.WhateverScope.
  </code></pre>
  
  <p>because it seems to insist that the <code>WhateverScope</code> is available all the time, not just in the child injector.</p></li>
  </ul>
  
  <p>All these problems seem to be due to the fact that I need to be able to configure whether to use <code>TestClass</code> or <code>RealClass</code> in the parent, but then to be able to instantiate them later, as a singleton, for a specific group of objects. I'm pulling my hair out over how to get this done!</p>
  
  <p>By the way, the documentation for Guice scopes is horrible and almost impossible to understand. <a href="http://andrewtill.blogspot.com/2011/09/guice-custom-scopes.html" rel="nofollow noreferrer">This article</a> is the only one that has gotten me anywhere:</p>