登録
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")));