I have a question regarding Guice. I have got an interface IMyInterface, the class MyClass implements it.
public class MyClass {
public MyClass(IMyWorker);
}
The interface IMyWorker has two implementaters MyWorker1 and MyWorker2.
The binding I have got is:
bind(IMyInterface.class).to(MyClass.class);
bind(IMyWorker.class).annotatedWith(W1.class).to(MyWorker1.class);
bind(IMyWorker.class).annotatedWith(W2.class).to(MyWorker2.class);
W1 and W2 are defined annotations.
My questions is that: When I create an instance of MyClass, how can I specify which Worker I want to inject into the constructor of MyClass?
Many thanks.
Add the annotation to the constructor of
MyClass.This way an instance of class
MyWorker1will be injected.