Suppose I have 5 classes A, B, C, D, E that all implement a common interface X. Each of the classes B, C, D and E has a field of type X (so they can be seen as wrapper classes).
Which instances are created is determined at runtime, so I could have for example one of the following object graphs:
E -> D -> C -> B -> A
D -> B -> A
E -> A
A
(The order is fixed and the innermost instance is always of type A, but otherwise there are no restrictions.)
No I’d like to create this objects with Guice to avoid providing all their other dependencies by hand.
What’s the easiest way to do this? Currently it seems that I have to
- let Guice create instance of A
- create a module that binds X.class to this instance and a child injector with this additional module
- let Guice create the next instance (e.g., of type B)
- repeat 2., now binding X.class to the instance of B
- repeat 3. and 4. until all objects are created
Is there an easier way? Could I perhaps somehow automatically register a new instance of a subclass of X as a binding for X each time it is created?
Edit: Clarification of “Which instances are created is determined at runtime“:
My current code looks like this:
X createX() {
X x = new A(dependencies);
if (useB) x = new B(x, some, dependencies);
if (useC) x = new C(x, further, dependencies);
if (useD) x = new D(x, dependency);
if (useE) x = new E(x, yet, other, dependencies);
return x;
}
The value of the flags useB, useC, useD, and useE comes from a properties file.
My main goal is to save providing all the dependencies to the constructors manually.
Edit: Solution:
I have added my own solution which I have found in the meantime. Thanks to all answerers!
A way to improve my solution would be to make it possible to remove the @InInstance annotation on the constructor parameters. I have experimented with type listeners, but I haven’t found a way to do this. Hints would be welcome.
I have developed the following solution to the problem:
First, I create a marker annotation
@InInstance, which takes aClassobject as value. I use this annotation to annotate the parameters of type X in all wrapper classes (i.e., B to E). Example:Now if I want for example an instance of the full chain (A to E), I bind
and then call
createInstance(X.class).This solves the problem of letting Guice create my instances and provide the dependencies.
Now for the problem of creating the appropriate bindings at runtime according to the properties file, I created a Guice extension that allows me to create “wrapper bindings”. Such a binding takes a type (here X), a wrapper implementation type (here one of B to E) and a base module, which needs to contain a binding for X. It then creates a new module that contains two bindings:
annotatedWith(InInstance(wrapper type))Such a module can then be used to override the bindings of the base module with
Modules.override(base module).with(wrapper module).With some nice Guice-style EDSL, this looks like:
And with some helper methods for common cases it looks like:
Each line creates the appropriate wrapper module and returns a new module containing the given module with some bindings overriden by the wrapper module.