I'd like to override a generic type binding, but I allways got the same "No implementation was bound
" error.
I'm using roboguice 3.
Here is a exemple of code that I use:
public interface IParser<I, O> {}
public class Parser1 implements IParser<String, String> {
IParser<String, String> mParser;
@Inject
public Parser1(IParser<String, String> parser) {
mParser = parser;
}
}
public class Parser2 extends Parser1 {
@Inject
public Parser2(IParser<String, String> parser) {
super(parser);
}
}
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser1>() {});
}
}
And here is my injector creation :
RoboGuice.getOrCreateBaseApplicationInjector(this,
RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(this),
Modules.override(new MyModule()).with(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser2>() {});
}
})
);
If I don't try to override it (only user Parser1), all is fine, when I override standard object with providers, it works well too, but not with TypeLiteral.
My error is :
com.google.inject.CreationException: Unable to create injector, see the following errors:
1) No implementation for IParser<String, String> was bound.
What am I doing wrong?
Thanks.
You should change definition of classes which are implemented your interface.
Try this:
public class Parser1<I, O> implements IParser<I, O> {
}
public class Parser2<I, O> extends Parser1<I, O> {
}
And then you can bind your interface to class by this way:
bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser1<String, String>() {});
I'm not shure, but it looks remarkable to me that a binding to a conrete instance is used instead to a Class
:
bind(new TypeLiteral>() {}).to(new TypeLiteral() {});
did you try
bind(new TypeLiteral<IParser<String, String>>() {}).to(Parser2.class});
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With