Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android RoboGuice 使用指南(9):Untargetted Bindings

編輯:Android 開發教學

在建立 Bindings 時,也可以不指定綁定目標,這種做法通常用於搭配 @ImplementedBy 與 @ProvidedBy(後文會介紹)的實類(Concrete classes 或 type)。Untargetted bindings 的目的,是通知 Injector 某個類別型別,讓 Injector 能預先準備好某個依賴。這類綁定不含 to 語句。

例如:

 bind(MyConcreteClass.class);
 bind(AnotherConcreteClass.class).in(Singleton.class);

不過,如果此時還需要同時使用 binding annotations,那就必須為綁定加上目標——即使是綁定到同一個實類也一樣,例如:

 bind(MyConcreteClass.class)
     .annotatedWith(Names.named("foo"))
     .to(MyConcreteClass.class);
 bind(AnotherConcreteClass.class)
     .annotatedWith(Names.named("foo"))
     .to(AnotherConcreteClass.class)
     .in(Singleton.class);

掌握這個差異,就能依實際需求在「省略目標」與「明確綁定」之間靈活取捨。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved