Google Guiceのアレコレ ―同一のインタフェースを持った複数のクラスの登録と取得―

http://groups.google.com/group/google-guice/browse_thread/thread/10cbc601380b8757/f2f414d8e69c40a5?lnk=gst&q=Using+annotation+on+injector+getInstance+call#f2f414d8e69c40a5

登録

binder.bind(IService.class).annotatedWith(Names.named("A")).to(ServiceA.class);
binder.bind(IService.class).annotatedWith(Names.named("B")).to(ServiceB.class);

自動でのインジェクション

とくに迷わないはず。

 public class HogeHoge {
  @Inject
  @Named("A")
  private IService iService;
 }

injectorからの取得

Keyをうまく使う。

 IService aService = injector.getInstance(Key.get(IService.class, Names.named("A")));
 IService bService = injector.getInstance(Key.get(IService.class, Names.named("B")));