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

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

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

[翻訳]Guice User's Guide ■18. Injecting Constant Values

定数値のインジェクト

When it comes to constant values, Guice gives special treatment to several types:

定数値が設定されると、Guiceはそれぞれの種類によって固有の処理を行います。

  • Primitive types (int, char, ...)
  • Primitive wrapper types (Integer, Character, ...)
  • Strings
  • Enums
  • Classes

  • プリミティブ型(intやcharなど)
  • プリミティブラッパー型(Integer、Characterなど)
  • ストリング型
  • 列挙型
  • クラス

First, when binding to constant values of these types, you needn't specify the type you're binding to. Guice can figure it out from the value. For example, given a binding annotation named TheAnswer:

まずそれらの種類の定数値がバインドされたとき、バインドしたtypeを特定する必要はありません。Guiceは値から型を見つけ出すことが出来ます。たとえば、TheAnswerという名前のアノテーションがバインドされたとします。

bindConstant().annotatedWith(TheAnswer.class).to(42);

Has the same effect as:

同じ効果が以下で得られます。

bind(int.class).annotatedWith(TheAnswer.class).toInstance(42);

When it comes time to inject a value of one of these types, if Guice can't find an explicit binding for a primitive type, it will look for a binding to the corresponding wrapper type and vice versa.

これらの種類内の一つの値をインジェクトする時に、Guiceがプリミティブ型に関して明示的なバインディングを見つけることができないと、対応するラッパー型としてバインディングを探します。それは逆もまた同様です。