在創(chuàng)建 Bindings 時(shí),也可以不給出綁定的目標(biāo),通常用于含有 @ImplementedBy 和 @ProvidedBy (后面介紹)的實(shí)類 (Concrete classes 或 type)。 Untargeted bindings 目的是通知 Injector 某個(gè)類類型,從而 Injector 可以預(yù)先準(zhǔn)備某個(gè)依賴。Untargetted Bindings 不含 to 語句。
例如:
bind(MyConcreteClass.class);
bind(AnotherConcreteClass.class).in(Singleton.class);
但如果此時(shí)需要同時(shí)使用 binding annotations 時(shí),需要為綁定添加目標(biāo),即使是綁定到同一個(gè)實(shí)類,如:
bind(MyConcreteClass.class)
.annotatedWith(Names.named("foo"))
.to(MyConcreteClass.class);
bind(AnotherConcreteClass.class)
.annotatedWith(Names.named("foo"))
.to(AnotherConcreteClass.class)
.in(Singleton.class);