Victor
Victor

Reputation: 63

Dagger 2 - Module Provide Singleton with

I'm using Dagger2 with Android and I came across the problem of how to build building Singleton using named variants.

Let's Say for my example that I want to build object C from B and A. A is not describe here but B has two version (niceB and notNiceB). I want to build C with notNiceB, how can I specify which bean to inject. @Named("notNiceB") B b in the parameters does not seem to work.

@Module
public class MyModule {

@Provides
@Singleton
C providesC(B b, A a) {
    // Omitted here
}

@Provides
@Singleton
@Named("niceB")
B providesNiceB() {
    // Omitted here
}

@Provides
@Singleton
@Named("notNiceB")
B providesNotNiceB() {
    // Omitted here
}
}

Upvotes: 1

Views: 611

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81529

@Provides
@Singleton
C c(@Named("notNiceB") B b, A a) {
    // Omitted here
}

This should work.

Upvotes: 1

Related Questions