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

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

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

[翻訳]Guice User's Guide ■30. Binding to Strings

文字列へのバインド

We try to avoid using strings whenever possible as they're prone to misspellings, not tool friendly, and so on, but using strings instead of creating custom annotations can prove useful for quick and dirty code. For these situations, Guice provides @Named and Names. For example, a binding to a string name like:

ストリングを使用するは、ツールとの相性が悪いなどの理由でスペルミスの傾向があるので、可能な限り避けられますが、カスタムアノテーションを作成する代わりにストリングを使用すれば、汚いけど迅速にコードが書けます。こういった状況のために、Guiceは@NamedとNamesを提供しています。 例えば、ストリング名を結合するには以下のようにします。

import static com.google.inject.name.Names.*;

...

bind(named("bob")).to(10);

Will match injection points like:

@Inject @Named("bob") int score;