Dependency injection 缺少依赖项错误经常发生
我使用GoogleGuice作为DI框架,将我的接口绑定到不同的实现。服务器正常运行,但每当我尝试调用资源类时,就会发生以下错误。这并不是说绑定永远不起作用,而是在大多数情况下它失败了。我不知道我错过了什么。我在下面给出了一些源代码 模块类Dependency injection 缺少依赖项错误经常发生,dependency-injection,jersey,guice,Dependency Injection,Jersey,Guice,我使用GoogleGuice作为DI框架,将我的接口绑定到不同的实现。服务器正常运行,但每当我尝试调用资源类时,就会发生以下错误。这并不是说绑定永远不起作用,而是在大多数情况下它失败了。我不知道我错过了什么。我在下面给出了一些源代码 模块类 public class ABCModule extends AbstractModule { @Override protected void configure() { bind(IConnection.class).to(ABCConnecti
public class ABCModule extends AbstractModule {
@Override
protected void configure() {
bind(IConnection.class).to(ABCConnection.class);
}
执行注入的资源类
public class Resource {
Logger log = LoggerFactory.getLogger(Resource.class);
private final IContact contact;
private final IContactFolder contactFolder;
@Inject
public MLContact(IConnection con, IContactFolder contactFolder) {
this.contact = contact;
this.con = con;
this.contactFolder = contactFolder;
}
//Resource class methods
..
......
}
我正在使用guice-3.0.jar
编辑1:
INFO: Initiating Jersey application, version 'Jersey: 1.17 01/17/2013 04:27 PM' Aug 8, 2013 9:54:59 AM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for constructor public com.motilink.server.services.MLContact(com.motilink.connector.IContact,com.motilink.connector.IConnection,com.motilink.connector.IContactFolder) at parameter index 0
SEVERE: Missing dependency for constructor public com.motilink.server.services.MLContact(com.motilink.connector.IContact,com.motilink.connector.IConnection,com.motilink.connector.IContactFolder) at parameter index 1
那是哪个错误?一般来说,Guice错误是非常具有描述性的,所以发布跟踪会有很大帮助。我已经在上面的编辑1中发布了我的错误日志。这可能会帮助您找到我的问题的解决方案。看起来您在资源类中发布了MLContact的构造函数。更重要的是,您只向我们展示了MLContact的@Inject注释双参数构造函数,而您发布的跟踪暗示了三参数构造函数。