为什么我尝试覆盖与Guice的通用绑定时出错? (TypeLiteral) [英] Why do I have an error when I try to override a generic binding with Guice? (TypeLiteral)
问题描述
我想重写一个泛型类型的绑定,但我总是得到相同的没有绑定实现
错误。
我正在使用roboguice 3。
以下是我使用的代码示例:
public interface IParser< I,O> {}
public class Parser1实现IParser< String,String> {
IParser< String,String> mParser;
@Inject
public Parser1(IParser< String,String> parser){
mParser = parser;
$ b $ public class Parser2 extends Parser1 {
@Inject
public Parser2(IParser< String,String> parser){
超级(解析器);
$ b $ public class MyModule extends AbstractModule {
@Override
protected void configure(){
bind(new TypeLiteral< IParser< ; String,String>>(){})。to(new TypeLiteral< Parser1>(){});
$ / code>
这里是我的注射器创建:
RoboGuice.getOrCreateBaseApplicationInjector(this,
RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(this),
Modules (new MyModule())。(new AbstractModule(){
@Override
protected void configure(){
bind(new TypeLiteral< IParser< String,String>>( ){})。(new TypeLiteral< Parser2>(){});
}
})
);
如果我不尝试覆盖它(仅用户Parser1),一切都很好,当我
我的错误是:
com.google.inject.CreationException:无法创建注入器,请参阅以下错误:
1)IParser没有实现< String,String>受到约束。
我做错了什么?
感谢。
您应该更改实现您的界面的类的定义。
试试这个:
public class Parser1< I,O>实现IParser< I,O> {
}
public class Parser2< I,O>扩展Parser1< I,O> {
}
然后你可以通过这种方式将你的接口绑定到类上:(新TypeLiteral< IParser< String,String>>(){})。to(new TypeLiteral< Parser1< String, String>(){});
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>() {});
这篇关于为什么我尝试覆盖与Guice的通用绑定时出错? (TypeLiteral)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!