我想重写泛型类型绑定,但我总是得到相同的"No implementation was bound“错误。
我用的是roboguice 3
下面是我使用的代码示例:
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>() {});
}
}这是我创建的注入器:
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>() {});
}
})
);如果我不尝试覆盖它(只有用户Parser1),一切都很好,当我用提供程序覆盖标准对象时,它也能很好地工作,但不能用TypeLiteral。
我的错误是:
com.google.inject.CreationException: Unable to create injector, see the following errors:
1) No implementation for IParser<String, String> was bound.我做错了什么?
谢谢。
发布于 2016-01-15 00:12:45
您应该更改实现您的接口的类的定义。
试试这个:
public class Parser1<I, O> implements IParser<I, O> {
}
public class Parser2<I, O> extends Parser1<I, O> {
}然后,您可以通过以下方式将您的接口绑定到类:
bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser1<String, String>() {});发布于 2016-01-13 22:18:44
我不是舒尔,但在我看来,使用到conrete实例的绑定而不是到Class的绑定是值得注意的
绑定( TypeLiteral>() {}).to(新建TypeLiteral() {});
你试过了吗
bind(new TypeLiteral<IParser<String, String>>() {}).to(Parser2.class});https://stackoverflow.com/questions/34176461
复制相似问题