2016-09-19 107 views
0

我创建工作,喷油器:谷歌吉斯无法与两个模块

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

请出示的构造函数(或者你注入的方式)'StorageHelperImpl'。 –

+0

另外,它可能是一个包问题?例如,你在两个不同的包中创建两个具有相同名称的类?我在这里想到'StorageModule'和'StorageHelper'。检查您的所有进口。 –

+0

@OlivierGrégoire,不...我只有一个班,每个班都有名字。我更新问题。 – bukashka101

回答

2

尝试使用

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

是的!是工作! – bukashka101

+0

@hunter你能解释一下在这种情况下'Modules.combine(...)'的工作原理吗?我只是惊呆了,我不明白! (也很高兴它帮助了bukashka101。) –