gpt4 book ai didi

java - Google Guice 配置错误(未绑定(bind)实现)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:37 26 4
gpt4 key购买 nike

对 Google Guice 框架有疑问

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

No implementation for java.util.List was bound. while locating java.util.List for field at Operator.carShops(Operator.java:17) while locating 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);
}
}}

请帮忙

最佳答案

您的模块注册依赖项似乎丢失了。您需要告诉 Guice 在请求接口(interface)时将使用哪个类。

import com.google.inject.AbstractModule;

public class SimpleModule extends AbstractModule {

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

其中 CarShopImplCarShop 接口(interface)的特定实现。

比方说,CarShopImpl 类的开头应该是:

public class CarShopImpl implements CarShop {

// Implementation
}

关于java - Google Guice 配置错误(未绑定(bind)实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45885031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com