基本接口:
public interface Registry<E> {
E method();
}
接口实现:
RegistryImpl<E> implements Registry<E> {
@Inject
RegistryImpl(...) {
}
@Override
public E method() {
(...)
}
}
用于绑定的数据对象的注册表:
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
@interface CustomDataObjectARegistry {
}
用于绑定的数据对象B注册表:
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
@interface CustomDataObjectBRegistry {
}
我要绑定CustomDataObjectARegistry到RegistryImpl<DataObjectA>
和CustomDataObjectBRegistry到RegistryImpl<DataObjectB>
,但一直没能找出语法。
bind(CustomDataObjectARegistry.class).to(new TypeLiteral<RegistryImpl<DataObjectA>>() {})
是给我一个“无法解析的方法。”
我想你想要做什么或者是:
bind(new TypeLiteral<Registry<DataObjectA>>() {}).to(new TypeLiteral<RegistryImpl<DataObjectA>>() {})
bind(new TypeLiteral<Registry<DataObjectB>>() {}).to(new TypeLiteral<RegistryImpl<DataObjectB>>() {})
//and then inject with generics:
Registry<DataObjectA> registryA;
Registry<DataObjectB> registryB;
要么:
bind(Registry.class).annotatedWith(CustomDataObjectARegistry.class).to(new TypeLiteral<RegistryImpl<DataObjectA>>() {})
bind(Registry.class).annotatedWith(CustomDataObjectBRegistry.class).to(new TypeLiteral<RegistryImpl<DataObjectB>>() {})
//and then inject with annotations:
@CustomDataObjectARegistry Registry registryA;
@CustomDataObjectBRegistry Registry registryB;
基本上你可以使用完整的通用标志来区分,也可以使用注释,但你不应该既需要。我建议通过仿制药区别。