I have created Guice binding annotations that allow me to bind two different instances of a class depending on the annotation e.g.:
bind(Animal.class).withAnnotation(Cat.class).toInstance(new Animal("Meow"));
bind(Animal.class).withAnnotation(Dog.class).toInstance(new Animal("Woof"));
I was hoping to be able to create a provider method that provides a List that is a dependency for one of my classes, but can’t figure out how to use the annotations for this:
@Provider
List<Animal> provideAnimalList() {
List<Animal> animals = new ArrayList<Animal>();
animals.add(@Cat Animal.class); // No, but this is what I want
animals.add(@Dog Animal.class); // No, but this is what I want
return animals;
}
So I was assuming that I would just be able to use the annotations in the argument to add() method of the List… but no.
How should I be approaching this? It seems to me it would be simpler simply to new the two instances of the Animal class and maybe this is not how the binding annotations were meant to be used.
I’d appreciate comments on the best use of the binding annotations in this scenario.
Thanks
If it is really what you want, here a working solution :
Annotations :
Output :
However :