Proper structure for dependency injection (using Guice)

Posted by David B. on Stack Overflow See other posts from Stack Overflow or by David B.
Published on 2011-11-23T02:21:13Z Indexed on 2011/11/23 17:50 UTC
Read the original article Hit count: 186

I would like some suggestions and feedback on the best way to structure dependency injection for a system with the structure described below. I'm using Guice and thus would prefer solutions centered around it's annotation-based declarations, not XML-heavy Spring-style configuration.

Consider a set of similar objects, Ball, Box, and Tube, each dependent on a Logger, supplied via the constructor. (This might not be important, but all four classes happen to be singletons --- of the application, not Gang-of-Four, variety.)

A ToyChest class is responsible for creating and managing the three shape objects. ToyChest itself is not dependent on Logger, aside from creating the shape objects which are.

The ToyChest class is instantiated as an application singleton in a Main class.

I'm confused about the best way to construct the shapes in ToyChest. I either (1) need access to a Guice Injector instance already attached to a Module binding Logger to an implementation or (2) need to create a new Injector attached to the right Module.

(1) is accomplished by adding an @Inject Injector injectorfield to ToyChest, but this feels weird because ToyChest doesn't actually have any direct dependencies --- only those of the children it instantiates.

For (2), I'm not sure how to pass in the appropriate Module.

Am I on the right track? Is there a better way to structure this?

The answers to this question mention passing in a Provider instead of using the Injector directly, but I'm not sure how that is supposed to work.

EDIT:

Perhaps a more simple question is: when using Guice, where is the proper place to construct the shapes objects? ToyChest will do some configuration with them, but I suppose they could be constructed elsewhere. ToyChest (as the container managing them), and not Main, just seems to me like the appropriate place to construct them.

© Stack Overflow or respective owner

Related posts about java

Related posts about dependency-injection