0

I have this interface classes:

public interface Query {
    String getQuery();
}

Query Builder implements Query

public class QueryBuilder implements Query {
    private String query;

    private final String table;

    @Inject
    public QueryBuilder(@Named("Table") String table) {
        this.table = table;
    }

    
    public Query insert() {
        query = "INSERT ";
        return this;
    }
    
    @Override
    public String getQuery() {
        return query;
    }
}

Table is an abstract Class use as the generic class

public class Table {

    protected Query query;

    @Inject
    public Table(@Named("Table") Query query) {
        this.query = query;
    }
}

UserTable extends Table as would do some other tables.

public class UserTable extends Table {
    private Query query;

    @Inject
    public UserTable(@Named("UserTable") Query query) {
        super(query);
    }

    public String insert() {
        return query.insert().query();
    }
}

This is the QueryModule

public class QueryModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(Query.class).annotatedWith(Names.named("UserTable")).to(QueryBuilder.class);
    }

    @Provides
    @Named("UserTable")
    public String provideUserTable() {
        return "users";
    }
}

Here I test my code in a main class.

public static void main(String[] args) {
        Injector injector = Guice.createInjector(new QueryModule());
        Table table = injector.getInstance(UserTable.class);
        System.out.println("Insert statement: " + table.insert());
    }

I'm getting this nasty error

Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:

Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:

1) [Guice/MissingImplementation]: No implementation for String annotated with @Named(value=Table) was bound.

Did you mean?
    * String annotated with @Named(value=Table1Query)

I want to make the table property dynamique for each Table using Guice Java. How can I do so ?

Nikodev
  • 13
  • 3
  • "Table is an abstract Class use as the generic class". This class neither abstract nor use generic types – Alexey R. May 22 '23 at 17:29

0 Answers0