-
Notifications
You must be signed in to change notification settings - Fork 2
Theory of Operation
The Pinject extension uses CDI's bean discovery events to participate in the dependency injection process. For each injection point that is qualified with @Property, the extension resolves the corresponding property name to
a string value, and converts the string representation to an instance of the target type of the injection point.
CDI dependency injection is based on the injection point type, with additional hints provided by qualifiers. All injection points of a given type with the same qualifiers are considered equivalent -- i.e. the same bean would be injected into each such injection point. This appears to create a problem, since a typical property injection point might look like this:
@Inject @Property
String myProperty;
@Inject @Property
String myOtherProperty;
How will CDI be able to distinguish beans of type String that are created by the extension? Without further qualification, both myProperty and myOtherProperty will be injected with the same bean of type String.
Pinject solves this problem by dynamically providing a unique qualifier to each injection point qualified by @Property. It assigns this same unique qualifier to the bean that represents the resolved property value. In this way, as far as CDI is concerned, each property value is uniquely qualified for injection into its targeted injection point.
In effect, it's as if we've qualified each of the injection points with a name to give it a unique identity and then defined a qualified producer method to inject the property value.
@Inject @Property(name = "myProperty")
String myProperty;
@Inject @Property(name = "myOtherProperty")
String myOtherProperty;
@Produces @Property(name="myProperty")
String getMyProperty() { return "foo" }
@Produces @Property(name="myOtherProperty")
String getMyOtherProperty() { return "bar" }
Of course, because Pinject is doing all of this automatically, we just need to put our qualified property names in a resource that Pinject can locate and it will figure out which properties get injected into which injection points, without requiring us to write producer methods or do the details of qualifying everything uniquely.