2
votes

I have a method annotated with @Provides in my Guice config, but it doesn't work.

public class GuiceConfig extends GuiceServletContextListener {

  ...

  @Provides @RequestScoped
  EntityManager provideEntityManger() {
    return entityManagerFactory.createEntityManager();
  }

}

When I run my application I get the following error:

com.google.inject.CreationException: Guice creation errors:<|<|1) No implementation for javax.persistence.EntityManager was bound.<| while locating javax.persistence.EntityManager<|
for parameter 0 at com.someclass.of.myproject

1
I could be wrong, but it looks like a scoping related issue to me. Are you sure that the scoping is correct? When your webapp starts, it won't be in RequestScope. - gpampara

1 Answers

5
votes

The main problem here is that you're using @Provides incorrectly. An @Provides method must be in one of the Modules that you use when initializing Guice... you can't just put it in the GuiceServletContextListener.