Google Guice配置错误(未实施已绑定)

时间:2017-08-25 15:41:48

标签: java dependencies guice inject

Google Guice框架存在问题

我正在尝试创建一个注入对象列表的简单应用程序。不幸的是,在尝试运行应用程序时,我收到以下错误。

  

没有绑定java.util.List的实现。     在查找java.util.List时       在Operator.carShops的字段(Operator.java:17)     找到Operator()

以下是程序代码:

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

请帮忙

1 个答案:

答案 0 :(得分:1)

似乎缺少注册依赖项的模块。您需要告诉Guice在要求接口时将使用哪个类。

import com.google.inject.AbstractModule;

public class SimpleModule extends AbstractModule {

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

CarShopImplCarShop接口的特定实现。

让我们说,CarShopImpl课程的开头应该是:

public class CarShopImpl implements CarShop {

    // Implementation
}