Google Guice无法使用两个模块

时间:2016-09-19 07:31:17

标签: java guice

我创建了注射器:

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

1 个答案:

答案 0 :(得分:2)

尝试使用

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