Tôi khá mới đối với Dagger 2. Tôi đang cố triển khai nó trong dự án Android của mình. Tôi có số Service
cần GoogleApiClient
. Tôi đang sử dụng Dagger để tiêm nó vào dịch vụ này.Dagger 2: Thành phần phụ thuộc vào nhiều hơn một thành phần phạm vi
@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
và RepositoryModule
có phương pháp đánh dấu @Singleton @Provides
Khi tôi xây dựng dự án của tôi, tôi nhận được lỗi này:
The locationServiceComponent depends on more than one scoped component: @Singleton NetComponent @Singleton RepositoryComponent
Tôi hiểu tôi LocationComponent
không thể phụ thuộc vào hai @Singleton
scoped thành phần nhưng tôi cần cả hai của họ trong dịch vụ của tôi và cả hai cần phải là @Singleton
.
Có cách nào khác thay thế tốt hơn để làm điều tương tự không?
"Tôi hiểu LocationComponent của tôi không thể phụ thuộc vào hai thành phần @Singleton scoped" -> Bạn có thể giải thích lý do tại sao điều này là không thể? –