Dependency injection 模棱两可的机器人腿有效吗?

Dependency injection 模棱两可的机器人腿有效吗?,dependency-injection,annotations,guice,robot-legs-problem,Dependency Injection,Annotations,Guice,Robot Legs Problem,在本文中,他们讨论了使用注释区分多个实例(类似于注释) 我的问题是:我可以将一个Impl绑定到一个没有注释的接口上,并将另一个Impl绑定到同一个带有注释的接口上吗?基本上,我的第一个impl将充当其他impl的容器 bind(SecurityAuthorizer.class).to(CompositeSecurityAuthorizer.class); bind(SecurityAuthorizer.class) .annotatedWith(Names.named(Component

在本文中,他们讨论了使用注释区分多个实例(类似于注释)

我的问题是:我可以将一个Impl绑定到一个没有注释的接口上,并将另一个Impl绑定到同一个带有注释的接口上吗?基本上,我的第一个impl将充当其他impl的容器

bind(SecurityAuthorizer.class).to(CompositeSecurityAuthorizer.class);
bind(SecurityAuthorizer.class)
    .annotatedWith(Names.named(ComponentAuthorizer.class.getSimpleName()))
    .to(ComponentAuthorizer.class).in(Singleton.class);

另外一个问题,我们使用命名(…)的名称是否被认为是不好的形式?只是试图避免创建大量注释类,但却希望能够获得重构的好处。

是的,这应该是可行的。您可能还想看看,它是为类似这样的场景设计的。复合实现将注入一组接口:

public class CompositeSecurityAuthorizer {
    @Inject 
    CompositeSecurityAuthorizer(Set<SecurityAuthorizer> authorizers) {
        ...
    }
}
公共类复合安全授权人{
@注入
复合安全授权人(集合授权人){
...
}
}

不幸的是,这是用于Gin模块的,因此还不支持多重绑定。