Guice 提供了所谓的绑定注解的两种变体,它们似乎真的分解为类和实例级别的注解:
“班级级别”:
bind(Service.class).annotatedWith(Red.class).to(RedServiceImpl.class);
@Red
public class SomeService implements Service { ... }
Service redSvc = injector.getInstance(SomeService.class);
“实例级”:
bind(Service.class).annotatedWith(Names.named("Blue").to(BlueServiceImpl.class);
@Blue blueSvc = injector.getInstance(Service.class);
什么时候一种方法优先于另一种?似乎类级别的注释比实例级别的更绝对/不灵活。任何一种方法的优点/缺点/警告/陷阱?