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

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

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

[翻訳]Guice User's Guide ■19. Converting Strings

文字列の変換

If Guice still can't find an explicit binding for one of the above types, it will look for a constant String binding with the same binding annotation and try to convert its value. For example:

Guiceが、上記の種類の中でも明示的なバインディングを見つけられなかったらGuiceは同じバインドアノテーションを持つString定数を探しに行き、その値を変換します。

bindConstant().annotatedWith(TheAnswer.class).to("42"); // String!

Will match:

これは以下のアノテーションに一致します。

@Inject @TheAnswer int answer;

When converting, Guice will try to look up enums and classes by name. Guice converts a value once at startup which also means you get up front type checking. This feature comes in especially handy if the binding value comes from a properties file for example.

変換がおこなわれるとき、GuiceEnumとClassを名前によって検索しようとします。Guiceは、起動時に1回だけ値を変換します。それは起動時に前もって型のチェックをするということも意味しています。この機能は、たとえばバインド値をプロパティーファイルから取得するような場合に特に便利です。