我对Dagger 2还不是很熟悉。我正试图在我的Android项目中实现它。
我有一个需要GoogleApiClient
的Service
。我正在使用Dagger将其注入到此服务中。
@FragmentScoped
@Component(dependencies = {NetComponent.class, RepositoryComponent.class})
public interface CustomServiceComponent {
void inject(CustomService customService);
}
@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
GoogleApiClient getGoogleApiClient();
}
@Singleton
@Component(modules = {AppModule.class, RepositoryModule.class})
public interface RepositoryComponent {
DatabaseService getDatabaseService();
}
AppModule
、NetModule
和RepositoryModule
中有被标记为@Singleton @Provides
的方法。
当我构建我的项目时,出现了以下错误:
LocationServiceComponent
依赖于不止一个作用域为@Singleton
的组件:@Singleton NetComponent @Singleton RepositoryComponent
我知道我的LocationComponent
不能依赖于两个@Singleton
作用域的组件,但是我需要它们两个在我的服务中,并且都需要是@Singleton
。
是否有更好的替代方案来实现同样的功能?