Google guice - 多重绑定 +仿制药 +辅助注射
我有以下类:
public interface Factory<T extends MyParentClass> {
public T create(String parameter);
}
public class FactoryImpl1 implements Factory<MyChildClass1> {
public MyChildClass1 create(String parameter){
...
}
}
public class FactoryImpl2 implements Factory<MyChildClass2> {
public MyChildClass2 create(String parameter){
...
}
}
public class MyModule extends AbstractModule {
@Override
protected void configure() {
MapBinder<String, Factory<MyParentClass>> factoryMap = MapBinder.newMapBinder(binder(), new TypeLiteral<String>() {}, new TypeLiteral<Factory<MyParentClass>>(){});
factoryMap.addBinding("myKey1").to(FactoryImpl1.class);
factoryMap.addBinding("myKey2").to(FactoryImpl2.class);
}
}
我的模块中的语法不正确,我不知道如何配置它。
事实上,我希望在我的工厂界面中为每种可能的情况建立一个工厂,
提前感谢您的帮助。
I have the following classes :
public interface Factory<T extends MyParentClass> {
public T create(String parameter);
}
public class FactoryImpl1 implements Factory<MyChildClass1> {
public MyChildClass1 create(String parameter){
...
}
}
public class FactoryImpl2 implements Factory<MyChildClass2> {
public MyChildClass2 create(String parameter){
...
}
}
public class MyModule extends AbstractModule {
@Override
protected void configure() {
MapBinder<String, Factory<MyParentClass>> factoryMap = MapBinder.newMapBinder(binder(), new TypeLiteral<String>() {}, new TypeLiteral<Factory<MyParentClass>>(){});
factoryMap.addBinding("myKey1").to(FactoryImpl1.class);
factoryMap.addBinding("myKey2").to(FactoryImpl2.class);
}
}
The syntax in my module is not correct and I don'know how to configure this.
In fact I would like to have a factory for each possible in my factory interface
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Factory
不是Factory
的超类型,您需要指定通配符泛型(在这种情况下绑定或未绑定并不重要,因为您已绑定它)在工厂定义中)。尝试在
此处检查泛型之间的超类型关系。
Factory<MyParentClass>
is not supertype ofFactory<MyChildClass1>
, you need to specify a wildcard generic (bound or unbound in this case doesn't matter as you have bound it in the Factory definition).Try with
Check here for supertype relationships between generics.