没有实现被绑定 - Java Guice
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24766665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No implementation was bound - Java Guice
提问by Lorena Nicole
Novice here trying to use a dummy Java Facebook app that uses Guice to inject a database dependency into the Facebook factory but continue to have Guice error out telling me:
新手在这里尝试使用一个虚拟的 Java Facebook 应用程序,该应用程序使用 Guice 将数据库依赖项注入 Facebook 工厂,但继续出现 Guice 错误告诉我:
### No implementation for com.example.storage.Db annotated with @com.example.storage.annotations.SystemDb() was bound while locating com.example.storage.Db annotated with @com.example.storage.annotations.SystemDb() for parameter 0 at com.example.facebook.client.exceptions.FacebookExceptionHandlerDb at com.example.facebook.client.guice.FacebookClientModule.configure
### Could not find a suitable constructor in com.example.facebook.statsd.StatsdClient. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at com.example.facebook.statsd.StatsdClient.class while locating com.example.facebook.statsd.StatsdClient for parameter 1 at com.example.facebook.client.exceptions.FacebookExceptionHandlerDb. com.example.facebook.client.guice.FacebookClientModule.configure
### com.example.storage.Db 没有实现 @com.example.storage.annotations.SystemDb() 在定位 com.example.storage.Db 时绑定@com.example.storage.annotations.SystemDb () 用于 com.example.facebook.client.exceptions.FacebookExceptionHandlerDb 处的参数 0,位于 com.example.facebook.client.guice.FacebookClientModule.configure
### 在 com.example.facebook.statsd.StatsdClient 中找不到合适的构造函数。类必须有一个(并且只有一个)用 @Inject 注释的构造函数或一个非私有的零参数构造函数。在 com.example.facebook.statsd.StatsdClient.class 中,同时在 com.example.facebook.client.exceptions.FacebookExceptionHandlerDb 处为参数 1 定位 com.example.facebook.statsd.StatsdClient。com.example.facebook.client.guice.FacebookClientModule.configure
Code for app:
应用程序代码:
app.java
应用程序.java
package com.example.facebook;
import com.google.inject.Guice;
import com.restfb.Connection;
import com.restfb.types.Post;
import com.example.facebook.client.FacebookClientFactory;
import com.example.facebook.client.RobustFacebookClient;
import com.example.facebook.client.guice.FacebookClientModule;
import com.example.facebook.statsd.StatsdClient;
public class App {
public static void main ( String[] args ) {
final FacebookClientFactory facebookClientFactory =
Guice.createInjector(new FacebookClientModule()).getInstance(FacebookClientFactory.class);
//error from line above
final RobustFacebookClient robustFacebookClient =
facebookClientFactory.create("accessToken");
//more ...
}
The resulting error points me to the FacebookClientModule
binding:
由此产生的错误将我指向FacebookClientModule
绑定:
FacebookClientModule.java
FacebookClientModule.java
public class FacebookClientModule extends AbstractModule {
bind(FacebookExceptionHandler.class).to(FacebookExceptionHandlerDb.class);
//error resulting from the failed binding on the FacebookExceptionHandlerDB class
install(new FactoryModuleBuilder()
.implement(FacebookClient.class, RobustFacebookClient.class)
.build(FacebookClientFactory.class));
}
}
}
Where inside the FacebookExceptionHandleDB
class the constructor has the injection:
在FacebookExceptionHandleDB
类中,构造函数具有注入的位置:
FacebookExceptionHandlerDB.java
FacebookExceptionHandlerDB.java
public class FacebookExceptionHandlerDb implements FacebookExceptionHandler {
// list of class String variables ...
private final FacebookErrorParser parser;
private final Db db;
private StatsdClient statsd;
@Inject
public FacebookExceptionHandlerDb(@SystemDb Db db, StatsdClient statsd, FacebookErrorParser parser) {
this.db = db;
this.statsd = statsd;
this.parser = parser;
}
}
From what I can gleam, the dependency injection for parameters zero and one, db
and statsD
respectively, is failing. Could someone point out where or what in the app code is missing?
从我可以闪光,依赖注入的参数0和1,db
以及statsD
分别失败。有人可以指出应用程序代码中缺少的位置或内容吗?
采纳答案by Miguel Lavigne
At first glance it seems like your missing the bindings for the Db annotated dependency and the StatsdClient.
乍一看,您似乎缺少 Db 注释依赖项和 StatsdClient 的绑定。
You'll need to provide the missing bindings to your module like so
您需要像这样为模块提供缺少的绑定
bind(Db.class).annotatedWith(SystemDb.class).to(DbImplOfSomeSort.class);
bind(StatsdClient.class).to(StatsdClientImplOfSomeSort.class);
Guice is able to automatically inject Concrete Class with either a public no argument constructor or a constructor with @Inject without any specific defined binding in your module but when it comes to Interfaces you have to define the necessary bindings.
Guice 能够自动注入具有公共无参数构造函数或带有 @Inject 的构造函数的具体类,而在您的模块中没有任何特定的定义绑定,但是当涉及到接口时,您必须定义必要的绑定。
Here Db.class and StatsdClient.class are interfaces which you need to bind to specific implementation.
这里 Db.class 和 StatsdClient.class 是您需要绑定到特定实现的接口。
回答by Nate Vaughan
Not the source of the issue in this particular case, but I ran across this issue when I had my implementation and interface classes backwards:
在这种特殊情况下不是问题的根源,但是当我的实现和接口类倒退时,我遇到了这个问题:
public class MyModule extends AbstractModule {
@Override
public void configure() {
bind(MyClassImpl.class).to(MyInterface.class);
}
}
Should have been:
本来应该:
bind(MyInterface.class).to(MyClassImpl.class);