Is there a way to tell Dagger 2 how to provide something, but not allow it to be injected?
Say I want to inject a Qux
. A Qux
requires a Foo
and a Bar
:
@Provides
@Singleton
Foo foo() {
return new Foo();
}
@Provides
@Singleton
Bar bar() {
return new Bar();
}
@Provides
@Singleton
Qux qux(final Foo foo, final Bar bar) {
return new Qux(foo, bar);
}
But what if I don't want Foo
and Bar
to be injectable? Perhaps exposing them would break the encapsulation of the Qux
, or perhaps they're factories of some kind that I only want the Qux
to have access to.
I've thought of a couple ways I could achieve this:
- If the
Foo
singleton is needed by other providers, I could make it a class member. But this would get messy ifFoo
has several dependencies of its own. - If the
Bar
singleton is not needed by any other providers, I could create the instance inside theQux
provider. But this would get messy ifQux
has several dependencies like that.
Neither of these solutions seem very elegant or Daggery. Is there another way?