lets say I have:
public interface Shape {}
public class Rectangle implements Shape {
}
public class Circle implements Shape {
}
and I have a ApplicationModule which needs to provides instances for both Rec and Circle:
@Module
public class ApplicationModule {
private Shape rec;
private Shape circle;
public ApplicationModule() {
rec = new Rectangle();
circle= new Circle ();
}
@Provides
public Shape provideRectangle() {
return rec ;
}
@Provides
public Shape provideCircle() {
return circle;
}
}
and ApplicationComponent:
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
Shape provideRectangle();
}
with the code the way it is - it won't compile.
error saying
Error:(33, 20) error: Shape is bound multiple times.
It makes sense to me that this can't be done, because the component is trying to find a Shape
instance, and it finds two of them, so it doesn't know which one to return.
My question is - how can I handle this issue?
question from:
https://stackoverflow.com/questions/39953933/dagger-2-two-provides-method-that-provide-same-interface 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…