|
@@ -29,31 +29,28 @@ import java.util.Map;
|
29
|
29
|
|
30
|
30
|
/**
|
31
|
31
|
* Facilitates simple injection of parameters into a constructor.
|
|
32
|
+ * <p>
|
|
33
|
+ * Injectors may be given a set of parent injectors, from which they will
|
|
34
|
+ * inherit parameters. No checking is performed to prevent circular references
|
|
35
|
+ * within parents.
|
32
|
36
|
*/
|
33
|
37
|
public class SimpleInjector {
|
34
|
38
|
|
35
|
|
- /** The parent injector, if any. */
|
36
|
|
- private final SimpleInjector parent;
|
|
39
|
+ /** The parent injectors, if any. */
|
|
40
|
+ private final SimpleInjector[] parents;
|
37
|
41
|
|
38
|
42
|
/** A mapping of known classes to the objects that should be injected. */
|
39
|
43
|
private final Map<Class<?>, Object> parameters
|
40
|
44
|
= new HashMap<Class<?>, Object>();
|
41
|
45
|
|
42
|
|
- /**
|
43
|
|
- * Creates a new injector with no parent.
|
44
|
|
- */
|
45
|
|
- public SimpleInjector() {
|
46
|
|
- this(null);
|
47
|
|
- }
|
48
|
|
-
|
49
|
46
|
/**
|
50
|
47
|
* Creates a new injector which will inherit injection parameters from
|
51
|
48
|
* the specified parent. Parent graphs must be acyclic.
|
52
|
49
|
*
|
53
|
|
- * @param parent The injector to inherit parameters from.
|
|
50
|
+ * @param parents The injectors to inherit parameters from.
|
54
|
51
|
*/
|
55
|
|
- public SimpleInjector(final SimpleInjector parent) {
|
56
|
|
- this.parent = parent;
|
|
52
|
+ public SimpleInjector(final SimpleInjector ... parents) {
|
|
53
|
+ this.parents = parents;
|
57
|
54
|
}
|
58
|
55
|
|
59
|
56
|
/**
|
|
@@ -93,8 +90,8 @@ public class SimpleInjector {
|
93
|
90
|
|
94
|
91
|
/**
|
95
|
92
|
* Retrieves a mapping of known injectable types to their corresponding
|
96
|
|
- * values. If this injector was created with a parent, this mapping will
|
97
|
|
- * also include mappings defined in the parent.
|
|
93
|
+ * values. This mapping will also include mappings defined in all of this
|
|
94
|
+ * injector's parents.
|
98
|
95
|
*
|
99
|
96
|
* @return A map of injectable parameters
|
100
|
97
|
*/
|
|
@@ -102,7 +99,7 @@ public class SimpleInjector {
|
102
|
99
|
final Map<Class<?>, Object> localParams
|
103
|
100
|
= new HashMap<Class<?>, Object>(parameters.size());
|
104
|
101
|
|
105
|
|
- if (parent != null) {
|
|
102
|
+ for (SimpleInjector parent : parents) {
|
106
|
103
|
localParams.putAll(parent.getParameters());
|
107
|
104
|
}
|
108
|
105
|
|