我正在使用 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)
有人知道发生了什么吗?我缺少什么?
万千封印
开满天机
相关分类