Locked History Actions

guice/Manual/Internals/BindingResolution

バインディングの解決

注入リクエストを解決するためのプロセスは、バインディングとタイプのアノテーションに依存している。 以下では、どのように注入リクエストを解決するかを説明する。

  1. 明示的なバインディングを使う
    • バインディングリンクがあればそれに従う。
    • バインディングがインスタンスを指示していればそれを返す。
    • バインディングがプロバイダを指示していればそれを使う。
  2. 親インジェクタに問い合わせる。 もしこのインジェクタが親インジェクタを持つ場合には、バインディングを解決するためにそれに問い合わせる。成功したなら、それを使う。そうでなければ次に進む。
  3. 複数の子インジェクタに問い合わせる。もしいずれかの子インジェクタがそのバインディングを所持しているのなら、ギブアップする。子インジェクタから取得したブラックリストを保持するため、子インジェクタへの参照を保持する必要はない。
  4. プロバイダ注入を扱う。型がProvider<T>であるとき、Tに対するバインディングがもしあればそれを使って解決を行う。

  5. 定数を変換する。If there is a constant string bound with the same annotation, and a TypeConverter that supports this type, use the converted String.

  6. If the dependency has a binding annotation, give up. Guice will not create default bindings for annotated dependencies.
  7. If the dependency is an array or enum, give up.
  8. Handle TypeLiteral injections. If the type is TypeLiteral<T>, inject that value, using context for the value of the type parameter.

  9. Use resolution annotations. If the dependency's type has @ImplementedBy or @ProvidedBy, lookup the binding for the referenced type and use that.

  10. If the dependency is abstract or a non-static inner class, give up.
  11. ただ一つの@Inject付コンストラクタ、あるいはpublicで引数無しのコンストラクタを使う。
    1. すべての依存についてのバインディングを検証する。コンストラクタパラメータ、@Inject付メソッドとフィールド、及びすべてのスーパータイプ。
    2. コンストラクタを起動する。
    3. すべてのフィールドに注入する。スーパータイプのフィールドはサブタイプより先に注入される。
    4. すべてのメソッドに注入する。スーパータイプのメソッドはサブタイプより先に注入される。