2010-11-18 32 views
2

我有一個在我的Guice配置中用@Provides註解的方法,但它不起作用。Guice @Provides方法不起作用

public class GuiceConfig extends GuiceServletContextListener { 

    ... 

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

} 

當我運行我的應用程序出現以下錯誤:

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

+2

我可能是錯的,但它看起來像一個範圍相關的問題給我。你確定該範圍是正確的嗎?當您的web應用程序啓動時,它將不在RequestScope中。 – gpampara 2010-11-18 12:48:14

回答

4

這裏的主要問題是,你正在使用@Provides不正確。一個@Provides方法必須在您初始化Guice時使用的Module之一中...您不能只將它放在GuiceServletContextListener中。