Google Guice 注入会员不工作

我正在使用 GoogleGuice 作为 DI 创建一个新项目。


所以我创建了一个我的 DAO 接口:


public interface UserDAO extends DAO<User> {


    // Some CRUD methods


}

和一个实现:


public class UserDAOImpl implements UserDAO {


     // CRUD Methods implementation


}

这是我的 ApplicationModule 类:


public class ApplicationModule extends AbstractModule {


    @Override

    protected void configure() {


        // Tried swap the order without results

        bind(UserDAO.class).in(Singleton.class);

        bind(UserDAO.class).to(UserDAOImpl.class);


    }


}

在我的 UserService 上,我尝试这样做:


@Inject

private UserDAO dao;

但我的 dao 始终为空。而且,当我调用Guice.createInjector(new ApplicationModule())UserService 构造函数时,我得到了以下堆栈跟踪:


Servlet.service() for servlet [Jersey REST Service] in context with path [/simple-rest-application] threw exception [A MultiException has 2 exceptions.  They are:

1. com.google.inject.CreationException: Unable to create injector, see the following errors:


1) No implementation for br.com.brunots.training.simple_rest_application.dao.UserDAO was bound.

  Did you mean?

    br.com.brunots.training.simple_rest_application.dao.UserDAO bound  at br.com.brunots.training.simple_rest_application.guice.ApplicationModule.configure(ApplicationModule.java:15)


  at br.com.brunots.training.simple_rest_application.guice.ApplicationModule.configure(ApplicationModule.java:14)


2) A binding to br.com.brunots.training.simple_rest_application.dao.UserDAO was already configured at br.com.brunots.training.simple_rest_application.guice.ApplicationModule.configure(ApplicationModule.java:15).

  at br.com.brunots.training.simple_rest_application.guice.ApplicationModule.configure(ApplicationModule.java:14)


有人知道发生了什么吗?我缺少什么?


慕的地6264312
浏览 136回答 2
2回答

万千封印

基本上你的问题是你试图将一个接口绑定到一个单例而不提供任何实现。然后在你的“答案”中,你实际上做得更好:你提供了一个带有实现的接口,然后你说你的实现是一个单例。但是,您实际上并没有使 成为UserDAO一个有效的单例,因为您为每个UserService.请尝试以下操作:ApplicationModule.javapublic class ApplicationModule extends AbstractModule {&nbsp; @Override protected void configure() {&nbsp; &nbsp; bind(UserDAO.class)&nbsp; &nbsp; &nbsp; &nbsp;// Define UserDAO&nbsp;&nbsp; &nbsp; &nbsp; .to(UserDAOImpl.class)&nbsp; // as implemented by UserDAOImpl&nbsp; &nbsp; &nbsp; .in(Singleton.class);&nbsp; &nbsp;// and make it a singleton.&nbsp; }}UserService.javapublic class UserService {&nbsp; private final UserDAO userDAO;&nbsp; @Inject UserService(UserDAO userDAO) { // Actually inject your UserDAO!!&nbsp; &nbsp; this.userDAO = userDAO;&nbsp; }}Main.javapublic class Main {&nbsp; public static void main(String[] args) {&nbsp; &nbsp; Injector injector = Guice.createInjector(new ApplicationModule());&nbsp; &nbsp; UserService userService = injector.getInstance(UserService.class);&nbsp; }}

开满天机

我发现了一些东西...如果我将 ApplicationModule 更改为:public class ApplicationModule extends AbstractModule {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void configure() {&nbsp; &nbsp; &nbsp; &nbsp; bind(UserDAO.class).to(UserDAOImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(UserDAOImpl.class).in(Singleton.class);&nbsp; &nbsp; }}这不能解决问题......我的 UserDAO 仍然没有被注入,但我可以用Guice.createInjector(new ApplicationModule()). 所以在 UserService 构造函数上,我可以做这样的事情:public UserService() {&nbsp; &nbsp; Injector injector = Guice.createInjector(new ApplicationModule());&nbsp; &nbsp; injector.injectMembers(this);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java