In the Guice FAQ, they talk about differentiating multiple instances with annotations (kind of).
My question: Can I bind a Impl to an Interface without an annotation, and bind another Impl to that same Interface with an annotation? Basically my first impl is going to act as a container for the others.
bind(SecurityAuthorizer.class).to(CompositeSecurityAuthorizer.class);
bind(SecurityAuthorizer.clas开发者_JAVA百科s)
.annotatedWith(Names.named(ComponentAuthorizer.class.getSimpleName()))
.to(ComponentAuthorizer.class).in(Singleton.class);
Bonus question, is our usage of Names.named(..) considered bad form? Just trying to avoid creating a ton of annotation classes, yet wanted the benefits of being able to refactor.
Yup, that should just work. You might want to also look at Multibindings, which is designed for scenarios like this one. The composite implementation would inject the set of interfaces:
public class CompositeSecurityAuthorizer {
@Inject
CompositeSecurityAuthorizer(Set<SecurityAuthorizer> authorizers) {
...
}
}
精彩评论