Let’s say I have a nontrivial tree of dependencies between classes. There are no cycles, but it goes several levels deep. At the very bottom there is a hibernate Session.
MyService -> MyDao -> Session
MyService -> MyCollaborator -> AnotherCollaborator
MyCollaborator -> MyDao
AnotherCollaborator -> MyDao
AnotherCollaborator -> AnotherDao -> Session
You get the point.
I would like to create the Session manually outside Guice and then use Guice to generate the entire graph from MyService down, using this Session. How can I do it?.
Sounds like you’re looking for a Provider?
EDIT
A provider is a user-defined factory that can be declared to guice when making bindings.
It allows the user to inject unspecified numbers of instances (by injecting the provider itself and invoking manually) or to supply interesting creation behavior, such as creating needed side-effects not carried out by the type’s own constructor.
It’s therefore a convenient tool for marrying other 3rd-party APIs into your dependencies: create a provider for each desired type and have it do whatever setup is required.
Concretely they are a generic interface providing a single
get()method, templated on the type you wish to manually create inside.