开发者

Are ambiguous robot legs valid?

开发者 https://www.devze.com 2023-02-17 21:33 出处:网络
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 sa

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号