com.google.inject.TypeLiteralの利用で最も多く使用されるのは、
型タイプ限定のインジェクトであろう。
List<String> みたいなものは。。。
Injector injector = Guice.createInjector(new AbstractModule(){
@Override
protected void configure(){
binder().bind(new TypeLiteral<List<String>>(){})
.toInstance(new ArrayList<String>());
}
}
);
Sample sample = injector.getInstance(Sample.class);
class Sample{
private List<String> list;
@Inject
public Sample(List<String> list){
this.list = list;
}
:
}
サンプルは、Listだが任意に作成するクラスで、Foo<T> のように宣言されるクラスを
用意した場合に型指定でインジェクトを限定できるのは cool!