【发布时间】:2012-01-22 05:54:05
【问题描述】:
我发现了其他相关问题,但没有一个这么简单:
如何使用 Guice 绑定以下泛型参数?
class A<T> {
T a;
@Inject A(T a) {
this.a=a;
}
}
【问题讨论】:
标签: generics dependency-injection guice
我发现了其他相关问题,但没有一个这么简单:
如何使用 Guice 绑定以下泛型参数?
class A<T> {
T a;
@Inject A(T a) {
this.a=a;
}
}
【问题讨论】:
标签: generics dependency-injection guice
public class TestGenericBinding {
static class A<T> {
T a;
@Inject A(T a) {
this.a=a;
}
}
@Test public void bindingWorked() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(123);
bind(new TypeLiteral<A<Integer>>() {});
}
});
A<Integer> a = injector.getInstance(
Key.get(new TypeLiteral<A<Integer>>(){}));
assertEquals(new Integer(123),a.a);
}
}
【讨论】: