Deletions are marked like this. | Additions are marked like this. |
Line 22: | Line 22: |
Guice will not construct nested classes unless they have the static modifier. Inner classes have an implicit reference to their enclosing class that cannot be injected. |
Guiceはネストされたクラスを作成しない、それらがstaticモディファイア付でない限り。 内部クラスはそのエンクロージングクラスへの暗黙的な参照を持つため、注入不可能である。 |
Line 26: | Line 27: |
Annotate types tell the injector what their default implementation type is. The @ImplementedBy annotation acts like a linked binding, specifying the subtype to use when building a type. |
アノテーションによって、デフォルトの実装タイプをインジェクタに伝えることができる。 @ImplementedByアノテーションは、サブタイプを指定することによってリンクバインディングのようにふるまう。 {{{ |
Line 33: | Line 35: |
The above annotation is equivalent to the following bind() statement: |
}}} 上記のアノテーションは以下のbind()と等価である。 {{{ |
Line 37: | Line 39: |
If a type is in both a bind() statement (as the first argument) and has the @ImplementedBy annotation, the bind() statement is used. The annotation suggests a default implementation that can be overridden with a binding. Use @ImplementedBy carefully; it adds a compile-time dependency from the interface to its implementation. |
}}} もし、一つのタイプがbind()指定されており、かつ@ImplementedByアノテーションを持つ場合には、bind()が優先する。 アノテーションはデフォルトの実装を指定するものであり、バインディングによってオーバライドすることができる。 @ImplementedByは注意して使うように、なぜならコンパイル時の依存をインターフェースと実装の間にもたらしてしまうからだ。 |
Line 42: | Line 46: |
@ProvidedBy tells the injector about a Provider class that produces instances: |
@ProvidedByはそのインスタンスを作成するプロバイダをインジェクタに通知する。 {{{ |
Line 49: | Line 53: |
The annotation is equivalent to a toProvider() binding: |
}}} 上記のアノテーションは、以下のtoProvider()バインディングと等価である。 {{{ |
Line 54: | Line 58: |
Like @ImplementedBy, if the type is annotated and used in a bind() statement, the bind() statement will be used. |
}}} @ImplementedByと同様に、両方ある場合はbind()が優先する。 |
その場バインディング
インジェクタがあるタイプのインスタンスを必要とするとき、バインディングを必要とする。 モジュールで定義されたバインディングのことを明示的バインディングと呼び、インジェクタはそれが提供されている場合はそれを使う。 タイプ(のインスタンス)が必要にも関わらず明示的バインディングが無い場合には、インジェクタは「その場バインディング」を試みる。 これらは、JITバインディングとか暗黙的バインディングなどとも呼ばれる。
コンストラクタバインディング
Guiceは、そのタイプの注入可能なコンストラクタを使うことにより、コンクリートな型に対するバインディングを作成する。 これらは、publicで引数なしのコンストラクタか、@Injectアノテーションのついたコンストラクタである。
public class PayPalCreditCardProcessor implements CreditCardProcessor { private final String apiKey; @Inject public PayPalCreditCardProcessor(@Named("PayPal API key") String apiKey) { this.apiKey = apiKey; }
Guiceはネストされたクラスを作成しない、それらがstaticモディファイア付でない限り。 内部クラスはそのエンクロージングクラスへの暗黙的な参照を持つため、注入不可能である。
@ImplementedBy
アノテーションによって、デフォルトの実装タイプをインジェクタに伝えることができる。 @ImplementedByアノテーションは、サブタイプを指定することによってリンクバインディングのようにふるまう。
@ImplementedBy(PayPalCreditCardProcessor.class) public interface CreditCardProcessor { ChargeResult charge(String amount, CreditCard creditCard) throws UnreachableException; }
上記のアノテーションは以下のbind()と等価である。
bind(CreditCardProcessor.class).to(PayPalCreditCardProcessor.class);
もし、一つのタイプがbind()指定されており、かつ@ImplementedByアノテーションを持つ場合には、bind()が優先する。 アノテーションはデフォルトの実装を指定するものであり、バインディングによってオーバライドすることができる。 @ImplementedByは注意して使うように、なぜならコンパイル時の依存をインターフェースと実装の間にもたらしてしまうからだ。
@ProvidedBy
@ProvidedByはそのインスタンスを作成するプロバイダをインジェクタに通知する。
@ProvidedBy(DatabaseTransactionLogProvider.class) public interface TransactionLog { void logConnectException(UnreachableException e); void logChargeResult(ChargeResult result); }
上記のアノテーションは、以下のtoProvider()バインディングと等価である。
bind(TransactionLog.class) .toProvider(DatabaseTransactionLogProvider.class);
@ImplementedByと同様に、両方ある場合はbind()が優先する。