简体   繁体   English

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

[英]Google Guice configuration error (No implementation was bound)

I have a problem with the Google Guice framework . 对Google Guice框架有疑问

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. 没有绑定java.util.List的实现。 while locating java.util.List for field at Operator.carShops(Operator.java:17) while locating Operator () 在定位Operator()时在Operator.carShops(Operator.java:17)中为字段定位java.util.List

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 请帮忙

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. 您需要告诉Guice在要求接口时将使用哪个类。

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. 其中CarShopImplCarShop接口的特定实现。

Let's say, the start of CarShopImpl class should be: 比方说, CarShopImpl类的开头应该是:

public class CarShopImpl implements CarShop {

    // Implementation
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM