Are ambiguous robot legs valid?

234 views Asked by At

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.class)
    .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.

1

There are 1 answers

1
Jesse Wilson On BEST ANSWER

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) {
        ...
    }
}