Dagger: Inject named string in constructor
You have two different names: stocks.codes
and stocks.code
.
You will also have to annotate your provideScanService
codes
parameter:
@Provides
@Singleton
public IScanService provideScanService(@Named("stocks.codes") String codes, IYahooService yahooService){
return new ScanService(codes, yahooService);
}
Or do it like this:
@Provides
@Singleton
public IScanService provideScanService(ScanService scanService){
return scanService;
}
If you mean Dagger 2, I can help you. First you have to declare dependencies in Component
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
@Named("cloud") UserDataSource userCloudSource();
@Named("disc") UserDataSource userDiscSource();
UserDataRepository userDataRepository();
}
then instantiate it in Module
@Module
public class ApplicationModule {
@Provides @Named("cloud")
UserDataSource provideCloudUserSource(UserCloudSource userSource) {
return userSource;
}
@Provides @Named("disc")
UserDataSource provideDiscUserSource(UserDiscSource userSource) {
return userSource;
}
@Provides
UserDataRepository provideUserRepository(UserDataRepository repository) {
return repository;
}
}
then inject it in constructor with @Named qualifiers
@Inject
public UserDataRepository(@Named("cloud") UserDataSource cloudSource,
@Named("disc") UserDataSource discSource) {
this.cloudDataSource= cloudSource;
this.discDataSource = discSource;
}