没有实现被绑定 - Java Guice [英] No implementation was bound - Java Guice

查看:33
本文介绍了没有实现被绑定 - Java Guice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的新手尝试使用一个虚拟的 Java Facebook 应用程序,该应用程序使用 Guice 将数据库依赖项注入 Facebook 工厂,但仍然出现 Guice 错误告诉我:

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:

### 使用@com.example.storage.annotations.SystemDb() 注释的com.example.storage.Db 没有实现在定位用@com.example.storage 注释的com.example.storage.Db 时绑定.com.example.facebook.client.exceptions.FacebookExceptionHandlerDb 处 com.example.facebook.client.guice.FacebookClientModule.configure 处的参数 0 的 annotations.SystemDb()

### 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

### 在 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

### 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

应用代码:

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 ...
    }

由此产生的错误将我指向 FacebookClientModule 绑定:

The resulting error points me to the FacebookClientModule binding:

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));
    }

}

构造函数在 FacebookExceptionHandleDB 类中注入的位置:

Where inside the FacebookExceptionHandleDB class the constructor has the injection:

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;
    }
}

据我所知,参数 0 和参数 1 的依赖注入分别是 dbstatsD 失败了.有人可以指出应用代码中缺少的位置或内容吗?

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?

推荐答案

乍一看,您似乎缺少 Db 注释依赖项和 StatsdClient 的绑定.

At first glance it seems like your missing the bindings for the Db annotated dependency and the 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 能够自动注入具有公共无参数构造函数或带有 @Inject 的构造函数的具体类,而在您的模块中没有任何特定的定义绑定,但是当涉及到接口时,您必须定义必要的绑定.

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.

这里的 Db.class 和 StatsdClient.class 是需要绑定到具体实现的接口.

Here Db.class and StatsdClient.class are interfaces which you need to bind to specific implementation.

这篇关于没有实现被绑定 - Java Guice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆