Rafal
Rafal

Reputation: 31

Google Guice configuration error (No implementation was bound)

I have a problem with the Google Guice framework.

I'm trying to create a simple application that injects a list of objects. Unfortunately, when trying to run an application, I get the following error.

No implementation for java.util.List was bound. while locating java.util.List for field at Operator.carShops(Operator.java:17) while locating Operator ()

Here is the program code:

public class Main {
public static void main(String[] args) {
        Injector injector = Guice.createInjector();
        Operator operator = injector.getInstance(Operator.class);

        operator.prepareData();
}}



public class Operator implements IOperator {
@Inject
private List<CarShop> carShops;

public List<CarShop> getCarShops() {
    return carShops;                             <--- Place of error occurrence
}

public void setCarShop(List<CarShop> carShops) {
    this.carShops = carShops;
}

public void prepareData() { 
    for(CarShop carShop:carShops)
        for(int i=0;i<10;i++) {
            Car car = new Car();
            car.setPrice(1000);     
            carShop.addCar(car);
        }
}}

Please help

Upvotes: 3

Views: 8247

Answers (1)

Nikolas
Nikolas

Reputation: 44496

It seems your module registering dependencies is missing. You need to tell Guice what class will be used when it is asked for an interface.

import com.google.inject.AbstractModule;

public class SimpleModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(CarShop.class).to(CarShopImpl.class);
    }
}

Where CarShopImpl is a particular implementation for CarShop interface.

Let's say, the start of CarShopImpl class should be:

public class CarShopImpl implements CarShop {

    // Implementation
}

Upvotes: 1

Related Questions