Sobes.tech
Junior — Middle

Is it possible to implement two methods in one module in Dagger that return the same type of object?

sobes.tech AI

Answer from AI

In Dagger, you cannot simply declare two methods in the same module that return the same type of object without additional qualification, as this will lead to a conflict during dependency resolution. To implement such a situation, you need to use @Qualifier or @Named annotations to distinguish these dependencies. For example:

@Module
public class MyModule {
    @Provides
    @Named("typeA")
    MyObject provideMyObjectA() {
        return new MyObject("A");
    }

    @Provides
    @Named("typeB")
    MyObject provideMyObjectB() {
        return new MyObject("B");
    }
}

Thus, when injecting, you can specify the desired variant using @Named("typeA") or @Named("typeB").