我 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);
}
}}
请帮忙
答案 0 :(得分:1)
似乎缺少注册依赖项的模块。您需要告诉Guice在要求接口时将使用哪个类。
import com.google.inject.AbstractModule;
public class SimpleModule extends AbstractModule {
@Override
protected void configure() {
bind(CarShop.class).to(CarShopImpl.class);
}
}
CarShopImpl
是CarShop
接口的特定实现。
让我们说,CarShopImpl
课程的开头应该是:
public class CarShopImpl implements CarShop {
// Implementation
}