【发布时间】:2014-12-16 00:12:19
【问题描述】:
我在尝试连接 guice 时遇到了错误。这是一些重现我所看到的问题的示例代码:
public class GuiceGenerics<K, V> {
public static interface Foo<K, V> {
}
public static class FooImpl<K, V> implements Foo<K, V> {
}
public static class FooModule extends PrivateModule {
@Override
protected void configure() {
bind(new TypeLiteral<Foo<String, Integer>>(){}).to(new TypeLiteral<FooImpl<String, Integer>>(){});
bind(new TypeLiteral<GuiceGenerics<String, Integer>>(){});
expose(new TypeLiteral<GuiceGenerics<String, Integer>>(){});
}
}
private Foo<K, V> foo;
@Inject
public GuiceGenerics(Foo<K, V> foo) {
this.foo = foo;
}
public static void main(String args[]) {
Injector injector = Guice.createInjector(new FooModule());
GuiceGenerics<String, Integer> guiceGenerics = injector.getInstance(GuiceGenerics.class);
}
}
当我运行 main() 时,我得到这个错误:
Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:
1) com.knewton.recommendationservice.retries.GuiceGenerics$Foo<K, V> cannot be used as a key; It is not fully specified.
at com.knewton.recommendationservice.retries.GuiceGenerics.<init>(GuiceGenerics.java:36)
while locating com.knewton.recommendationservice.retries.GuiceGenerics
1 error
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at com.knewton.recommendationservice.retries.GuiceGenerics.main(GuiceGenerics.java:42)
我感觉我在错误地使用 TypeLiteral,但我无法弄清楚我到底做错了什么。有什么建议吗?
【问题讨论】:
标签: java generics guice typeliteral