all
I have some questions regarding Google Guice. Any help is appreciated.
I have a handler interface and an implementation of it.
public interface Handler {
void handle();
}
public class HandlerImpl implements Handler {
private Filter filterOne;
@Override
void handler() {
filterOne.foo();
}
}
Filter is another interface:
public interface Filter {
void foo();
}
There are multiple implementation of it.
public class FilterOne implements Filter {
void foo() {
}
}
public class FilterTwo implements Filter {
void foo() {
}
}
Then in my Guice module:
public class HandlerModule extends AbstractModel {
@Override
protected void configure() {
bind(Handler.class).to(HandlerImpl.class);
}
@Provides
@Singleton
public Handler provideHandler(@Named("filterOne")filter filterOne) {
return new HandlerImpl(filterOne);
}
@Provides
@Singleton
@Named("filterOne")
public Filter provideFilterOne() {
return new FilterOne();
}
@Provides
@Singleton
@Named("filterTwo")
public Filter provideFilterTwo() {
return new FilterTwo();
}
}
With above implementation, I'm always getting the error - Could not find a suitable constructor in HandlerImpl. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
I'm using @Named to differ two filters. Am I using it wrong? Is it because I have two implementations of Filter and the Guice cannot tell which one to use when try to provide the HandlerImpl?