我有几个类实现 interface Provider<Communication>
我正在使用 Guice 和 @Named
注解以根据需要绑定(bind)它们,例如:
@Singleton
public class Verizon implements Provider<Call> {
...
}
@Singleton
public class TMobile implements Provider<Call> {
...
}
bind (new TypeLiteral<Provider<Call>>() {}).annotatedWith(
Names.named("Verizon")).to(Verizon.class);
bind (new TypeLiteral<Provider<Call>>() {}).annotatedWith(
Names.named("TMobile")).to(TMobile.class);
是否有一种干净的方法来实现将名称作为参数的工厂,例如:
public static <C extends Communication> Provider<C> getCallProvider(C communication) {
String providerName = communication.getProviderName();
return [Guice's matching object for type Provider<?> and @Named = providerName];
}
我尝试使用 Injector,但 Guice 不会将泛型作为 TypeLiteral 的参数:
public <C extends Communication> Provider<C> getCommunicationProvider(C communication) {
return injector.getInstance(Key.get(new TypeLiteral<CommunicationProvider<C>>() {},
Names.named(communication.getProvider().getId())));
}
这会抛出:
com.google.inject.ConfigurationException: Guice configuration errors:
1) Provider<C> cannot be used as a key; It is not fully specified.
最佳答案
提供商由 Guice 管理;当您绑定(bind)Foo
时或Provider<Foo>
正确的话,您应该可以请求 Foo
或Provider<Foo>
无需任何额外的工作。因此,您可能不希望这样:
bind (new TypeLiteral<Provider<Call>>() {}).annotatedWith(
Names.named("Verizon")).to(Verizon.class);
相反,您可能想要这个:
bind(Call.class).annotatedWith(Names.named("Verizon")).toProvider(Verizon.class);
...这将让你注入(inject) @Named("Verizon") Provider<Call>
还有@Named("Verizon") call
。那时您最初的请求就像这样简单:
/**
* Because of erasure, at compile time the injector can only guarantee that it
* returns something that extends Communication, not necessarily C. The cast and
* @SuppressWarnings will help with that.
*/
@SuppressWarnings("unchecked")
public static <C extends Communication> Provider<C> getCommunicationProvider(
C communication) {
return (Provider<C>) injector.getProvider(Key.get(communication.getClass(),
Names.named(communication.toString())));
}
另请注意,由于删除,没有其他方法可以获取 C 类型的类文字,因此使用 Call
的模拟或动态代理将会失败。
如果你想绑定(bind)SomeOtherInterface<Call>
而不是Provider<Call>
,您仍然可以这样做,但是您需要使用 Guice 的 Types
util class 动态创建 ParameterizedType并将其用作 Key#get(Type, Annotation)
的输入。有关创建 ParameterizedType 的更多背景信息实现,请阅读 this SO answer .
关于java - 使用 Guice 按类型和注释检索单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18968410/