No implementation was tied - Java Guice

A newbie here is trying to use a dummy Facebook Java application that uses Guice to embed database dependencies in the Facebook factory, but still has a Guice error telling me:

### There is no implementation for com.example.storage.Db annotated using @ com.example.storage.annotations.SystemDb () was linked when searching for com.example.storage.Db annotated using @ com.example.storage . annotations.SystemDb () for parameter 0 in com.example.facebook.client.exceptions.FacebookExceptionHandlerDb in com.example.facebook.client.guice.FacebookClientModule.configure

### Could not find a suitable constructor in com.example.facebook.statsd.StatsdClient. Classes must have one (and only one) constructor annotated using @Inject or a constructor with a null argument that is not private. at com.example.facebook.statsd.StatsdClient.class when searching for com.example.facebook.statsd.StatsdClient for parameter 1 in com.example.facebook.client.exceptions.FacebookExceptionHandlerDb. com.example.facebook.client.guice.FacebookClientModule.configure

Code for the application:

app.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 error I received indicates the binding FacebookClientModule:

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 class the FacebookExceptionHandleDBconstructor has an injection:

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

, , , db statsD , . - , ?

+4
2

, Db StatsdClient.

bind(Db.class).annotatedWith(SystemDb.class).to(DbImplOfSomeSort.class);
bind(StatsdClient.class).to(StatsdClientImplOfSomeSort.class);

Guice Concrete Class public @Inject - , , .

Db.class StatsdClient.class - , .

+11

, , :

public class MyModule extends AbstractModule {
    @Override
    public void configure() {
        bind(MyClassImpl.class).to(MyInterface.class);
    }
}

:

bind(MyInterface.class).to(MyClassImpl.class);
0

Source: https://habr.com/ru/post/1548548/


All Articles