gpt4 book ai didi

java - Google Guice 无法使用两个模块

转载 作者:行者123 更新时间:2023-12-02 03:21:21 25 4
gpt4 key购买 nike

我创建注入(inject)器:

private static Injector mvpInjector = Guice.createInjector(
HandlingModule.getInstance(),
StorageModule.getInstance()
);

然后我尝试获取类的实例:

mvpInjector.getInstance(ImageCreatePresenter.class).showImageCreateWindow();

这是类的构造函数,需要注入(inject):

@Inject
public ImageCreatePresenterImpl(ImageCreateView imageCreateView, StorageHelper storageHelper, ImageCreater imageCreater) {
this.imageCreateView = imageCreateView;
this.storageHelper = storageHelper;
this.imageCreater = imageCreater;
}

但是,在注入(inject)期间,抛出异常:

Exception in thread "main" com.google.inject.CreationException: Unable to create injector, see the following errors:

1) No implementation for com.dugin.rostislav.StorageHelper was bound.
while locating com.dugin.rostislav.StorageHelper
for parameter 1 at com.dugin.rostislav.presenter.ImageCreatePresenterImpl.<init>(ImageCreatePresenterImpl.java:26)
at com.dugin.rostislav.modules.HandlingModule.configure(HandlingModule.java:32)
<小时/>

我的模块:

1.

public class HandlingModule extends AbstractModule {
private static final HandlingModule module = new HandlingModule();

private HandlingModule() {
//Closed constructor
}

public static HandlingModule getInstance() {
return module;
}

@Override
protected void configure() {
bind(MainWindowPresenter.class).to(MainWindowPresenterImpl.class).in(Singleton.class);
bind(ImageCreatePresenter.class).to(ImageCreatePresenterImpl.class).in(Singleton.class);

bind(ImageCreater.class).to(ImageCreaterImpl.class).in(Singleton.class);

bind(ImageCreateView.class).to(ImageCreateViewImpl.class).in(Singleton.class);
bind(MainView.class).to(MainViewImpl.class).in(Singleton.class);
}
}

2.

public final class StorageModule extends AbstractModule {
private final static StorageModule module = new StorageModule();

private TypeListener storageHelperListener = new TypeListener() {
@Override
public <I> void hear(final TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
typeEncounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(Object o) {
if (o instanceof StorageHelper) {
try {
((StorageHelper) o).initDB();
} catch (DBInitializationException e) {
throw new RuntimeException(e);
}
}
}
});
}
};

private StorageModule() {
//Closed constructor
}

public static StorageModule getInstance() {
return module;
}

@Override
protected void configure() {
bind(ImageDB.class).to(SQLiteDB.class).in(Singleton.class);
bind(StorageHelper.class).to(StorageHelperImpl.class).in(Singleton.class);
bind(ImagesHelper.class).to(ImagesHelperImpl.class).in(Singleton.class);

bind(String.class)
.annotatedWith(Names.named(StorageModuleConst.DB_FOLDER_PATH))
.toProvider(Providers.of(StorageConst.OSLOADER_DATA_FOLDER));
bind(String.class)
.annotatedWith(Names.named(StorageModuleConst.IMAGES_FOLDER_PATH))
.toProvider(Providers.of(StorageConst.OSLOADER_IMAGES_FOLDER));

bindListener(Matchers.any(), storageHelperListener);
}
}

我的类(class)不存在!

<小时/>

为什么我会出错以及如何修复?

<小时/>

StorageHelperImpl构造函数:

    @Inject
public StorageHelperImpl(@NonNull ImageDB database, @NonNull ImagesHelper imagesHelper) {
this.database = database;
this.imagesHelper = imagesHelper;
}

最佳答案

尝试使用

Guice.createInjector(Modules.combine(HandlingModule.getInstance(),  StorageModule.getInstance()));

关于java - Google Guice 无法使用两个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567618/

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