Using HK2 in Java, is there a way to bind multiple instances of the same implementation class to the same contract with different qualifiers and different constructor arguments.
Here is a contrived example:
@Service
@Singleton
public class Circle extends Shape
{
@Inject
public Circle(final Pen pen)
{
// This pen could be RED or BLUE etc.
this.pen = pen;
}
}
bind(Circle.class)
.to(Shape.class)
.named("red circle")
// I would like something like this.
// This would inject a "Pen" instance qualified by "red" into the Circle.
.withConstructorArg(0, Pen.class, "red");
bind(Circle.class)
.to(Shape.class)
.named("blue circle")
// This would inject a "Pen" instance qualified by "blue" into the Circle.
.withConstructorArg(0, Pen.class, "blue");
// Then later in the code I can inject either one.
@Inject
public void MyCtor(@Named("red circle") Shape red, @Named("blue circle") Shape blue)
...
So far, the only way I have seen to accomplish this is to make a base class and then create a separate leaf classes that extends the base class (one for EACH variant).
Is there a better way to do this?