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

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

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

[翻訳]Guice User's Guide ■13. Annotating Bindings

注釈によるバインディング

If you need multiple bindings to the same type, you can differentiate the bindings with annotations. For example, to bind an interface Service and annotation @Blue to the concrete implementation BlueService, call:

複数のバインドを同じTypeに対しておこなう必要があるなら、アノテーションを使ってバインドすることでBindingを区別することができます。

bind(Service.class)
  .annotatedWith(Blue.class)
  .to(BlueService.class);

This binding matches the following the method:

このバインディングは以下のメソッドに一致します。

@Inject
void injectService(@Blue Service service) {
  ...
}

Notice that while @Inject goes on the method, binding annotations such as @Blue go directly on the parameter. The same goes for constructors. When using field injection, both annotations can apply directly to the field, as in this example:

@Injectがメソッドを実装し続ける間、@Blueのようなバインディングアノテーションが、パラメータに直接に作用することに注意してください。同じことはコンストラクタでも成立します。フィールドインジェクションを行う場合は、次の例のように両方のアノテーションをフィールドに対して直接適用することができます。

@Inject @Blue Service service;