Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 965
Rafal Avatar asked Aug 19 '26 14:08

Rafal


1 Answers

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
}
like image 67
Nikolas Charalambidis Avatar answered Aug 21 '26 02:08

Nikolas Charalambidis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!