繁体   English   中英

Google Guice无法使用两个模块

[英]Google Guice can't work with two modules

我创建注射器:

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

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

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

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

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

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

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

我的班级不存在!


为什么我会报错以及如何解决?


StorageHelperImpl构造函数:

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

尝试使用

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

暂无
暂无

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

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