title: Guice multiple implementations, parameterized constructor with dependencies
body: <p>I'm struggling with a particular dependency injection problem and I just can't seem to figure it out. FYI: I'm new to guice, but I have experience with other DI frameworks - that's why I believe this shouldn't be to complicated to achieve.</p>
  
  <p>What am I doing:
  I'm working on Lagom multi module project and using Guice as DI.</p>
  
  <p>What I would like to achieve:
  Inject multiple named instances of some interface implementation (lets' call it publisher, since it will publishing messages to kafka topic) to my service.
  This 'publisher' has injected some Lagom and Akka related services (ServiceLocator, ActorSystem, Materializer, etc..).</p>
  
  <p>Now I would like to have two instances of such publisher and each will publish messages to different topic (So one publisher instance per topic).</p>
  
  <p>How would I achieve that?
  I have no problem with one instance or multiple instances for the same topic, but if I want to inject different topic name for each instance I have a problem.</p>
  
  <p>So my publisher implementation constructor looks like that:</p>
  
  <pre><code>@Inject
  public PublisherImpl(
      @Named("topicName") String topic,
      ServiceLocator serviceLocator,
      ActorSystem actorSystem,
      Materializer materializer,
      ApplicationLifecycle applicationLifecycle) {
  ...
  }
  </code></pre>
  
  <p>If I want to create one instance I would do it like this in my ServiceModule:</p>
  
  <pre><code>public class FeedListenerServiceModule extends AbstractModule implements ServiceGuiceSupport {
      @Override
      protected void configure() {
          bindService(MyService.class, MyServiceImpl.class);
          bindConstant().annotatedWith(Names.named("topicName")).to("topicOne");
          bind(Publisher.class).annotatedWith(Names.named("publisherOne")).to(PublisherImpl.class);
      }
  }
  </code></pre>
  
  <p>How would I bind multiple publishers each for it's own topic?</p>
  
  <p>I was playing around with implementing another private module:</p>
  
  <pre><code>public class PublisherModule extends PrivateModule {
  
      private String publisherName;
      private String topicName;
  
      public PublisherModule(String publisherName, String topicName) {
          this.publisherName = publisherName;
          this.topicName = topicName;
      }
  
      @Override
      protected void configure() {
          bindConstant().annotatedWith(Names.named("topicName")).to(topicName);
          bind(Publisher.class).annotatedWith(Names.named(publisherName)).to(PublisherImpl.class);
      }
  }
  </code></pre>
  
  <p>but this led me nowhere since you can't get injector in you module configuration method:</p>
  
  <pre><code>Injector injector = Guice.createInjector(this); // This will throw IllegalStateException : Re-entry is not allowed
  injector.createChildInjector(
      new PublisherModule("publisherOne", "topicOne"),
      new PublisherModule("publisherTwo", "topicTwo"));
  </code></pre>
  
  <p>The only solution which is easy and it works is that I change my PublisherImpl to abstract, add him abstract 'getTopic()' method and add two more implementations with topic override.</p>
  
  <p>But this solution is lame. Adding additional inheritance for code reuse is not exactly the best practice. Also I believe that Guice for sure must support such feature.</p>
  
  <p>Any advises are welcome.
  KR, Nejc</p>