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

查看:81
本文介绍了没有实现绑定 - 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注释的com.example.storage.Db时,没有使用@ com.example.storage.annotations.SystemDb()注释的com.example.storage.Db的实现。 example.storage.annotations.SystemDb(),参见com.example.facebook.client.exceptions.FacebookExceptionHandlerDb中的参数0,位于com.example.facebook.client.guice.FacebookClientModule.configure

### 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注释的构造函数或一个非私有的零参数构造函数。 at 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 绑定:

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

从我可以看到的,参数的依赖注入零和一, db statsD 分别失败。有人可以指出应用程序代码中缺少的位置或内容吗?

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的构造函数自动注入Concrete 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.

这里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天全站免登陆