レベルエンター山本大のブログ

面白いプログラミング教育を若い人たちに

BLOCKVROCKリファレンス目次はこちら

[翻訳]Guice User's Guide ■9. Runtime

実行時

We can now use the injector we created during the first stage to inject objects and introspect on our bindings. Guice's runtime model consists of an injector which contains some number of bindings.

これで、オブジェクトを注入したり、バインドのときにintrospect(内省:reflection機能でクラスの内部情報を取得すること)したりするために、はじめのステージでつくったinjectorを使うことが出来ます。そして結合のときに、します。Guiceの実行時モデルは、いくつかのBindingオブジェクトを含むInjectorから成ります。

A Key uniquely identifies each binding. The key consists of a type which the client depends on and an optional annotation. You can use an annotation to differentiate multiple bindings to the same type. The key's type and annotation correspond to the type and annotation at a point of injection.

KeyはそれぞれのBindingオブジェクトのユニークな識別子です。Keyは、クライアントが依存するTypeとオプショナルなアノテーションから成ります。同じTypeを表す複数のBindingオブジェクトを区別するためのアノテーションを使うことが出来ます。KeyのTypeとアノテーションは、インジェクションポイントのTypeとアノテーションに対応します。

Each binding has a provider which provides instances of the necessary type. You can provide a class, and Guice will create instances of it for you. You can give Guice an instance of the type you're binding to. You can implement your own provider, and Guice can inject dependencies into it.

それぞれの結合(Binding)は 必要なTypeのインスタンスを提供するプロバイダーをもっています。Guiceは、開発者が提供したクラスのインスタンスを作ります。開発者は、GuiceにバインドするためのTypeのインスタンスを与えることが出来ます。開発者は独自のプロバイダーを実装することができます。そして、Guiceはその中に依存性を注入することができます。

Each binding also has an optional scope. Bindings have no scope by default, and Guice creates a new instance for every injection. A custom scope enables you to control whether or not Guice creates a new instance. For example, you can create one instance per HttpSession.

それぞれの結合は、オプショナルなスコープも持っています。Bindingオブジェクトは、デフォルトではスコープをもっていません。そして、Guiceはあらゆる注入のために新しいインスタンスを作ります。カスタムスコープは、Guiceが新しいインスタンスを作るかどうかを制御できるようにします。