title: Injecting EntityManager with Guice
body: <p>after I received a very good answer in my post about Guice injection in general, I wanted to know, if you could also help me with injection of EntityManagers in Guice.</p>
  
  <p>I already read the article <a href="https://github.com/google/guice/wiki/JPA" rel="nofollow noreferrer">https://github.com/google/guice/wiki/JPA</a> about Guice JPA. My code looks like this (I need the providers for my work):</p>
  
  <p>Provider:</p>
  
  <pre><code>public class DatabaseControllerProvider implements Provider&lt;DatabaseControllerInterface&gt; {
  
  
      private final Provider&lt;EntityManager&gt; entityManagerProvider;
  
      @Inject
      public DatabaseControllerProvider(final Provider&lt;EntityManager&gt; manager) {
          this.entityManagerProvider = manager;
      }
  
      @Override
      public DatabaseControllerInterface get() {
          return new DatabaseControllerImpl(entityManagerProvider.get());
      }
  }
  </code></pre>
  
  <p>Constructor of DatabaseControllerImplementation:</p>
  
  <pre><code>public class DatabaseControllerImpl implements DatabaseControllerInterface {
  
      private final EntityManager em;
  
      @Inject
      public DatabaseControllerImpl(final EntityManager manager) {
          this.em = manager;
      }
  }
  </code></pre>
  
  <p>Top level I call this:</p>
  
  <pre><code>public LogInFront() {
          final Injector injector = Guice.createInjector(new ModuleBinder(), new JpaPersistModule("ATVPersistence"));
          final PersistService persistService = persistenceInjector.getInstance(PersistService.class);
          persistService.start();
          logInService = injector.getInstance(LogInServiceInterface.class);
  }
  </code></pre>
  
  <p>to instantiate the JPAPersistService with my database.</p>
  
  <p>I receive an error, that <code>no Implementation for EntityManager was bound</code>, but if I call</p>
  
  <pre><code>...
  public LogInFront() {
          final Injector injector = Guice.createInjector(new JpaPersistModule("ATVPersistence"));
          final PersistService persistService = persistenceInjector.getInstance(PersistService.class);
          persistService.start();
          logInService = injector.getInstance(LogInServiceInterface.class);
  }
  </code></pre>
  
  <p>The instantiation works correctly, database works fine, and everything is nice and easy.</p>
  
  <p>So my guess is, that there's something wrong with my GuiceModule. As seen above, when I'm not calling the <code>new ModuleBinder()</code>, everything works just fine.</p>
  
  <pre><code>public class ModuleBinder extends AbstractModule {
      @Override
      protected void configure() {
          bind(DatabaseControllerInterface.class).asEagerSingleton();
          bind(AnalyzerInterface.class).asEagerSingleton();
          bind(SystemAdministrationInterface.class).asEagerSingleton();
          bind(LogInServiceInterface.class).asEagerSingleton();
      }
  }
  </code></pre>
  
  <p>-----------------------------EDIT-------------------------------------</p>
  
  <p>Rewrote above section.</p>
  
  <p>Tl;dr edition:
  ModuleBinder messes up Injection of EntityManager -why?</p>
  
  <p>---------------------------Found error-------------------------------</p>
  
  <p>I feel kind of dumb, I found the error. When taking a look into the <code>ModuleBinder</code> you can see, that it binds everything as <code>EagerSingleton</code>, but obviously, the <code>EntityManager</code> does not exist yet (the PersistService is started after creating the Injector). I created a new post on this here: <a href="https://stackoverflow.com/questions/47181835/jpapersistencemodule-guice-injector">https://stackoverflow.com/questions/47181835/jpapersistencemodule-guice-injector</a></p>
  
  <p>May it help other people!</p>
  
  <p>Best regards,</p>
  
  <p>JosefRucksack</p>