使用 Spring,您可以定义一个数组属性,并让 Spring 注入(inject)从给定类型派生的每个 (@Component) 类之一。
在 Guice 中有对应的吗?或者添加此行为的扩展点?
最佳答案
这看起来像是 Guice 的用例 MultiBinder .你可以有这样的东西:
interface YourInterface {
...
}
class A implements YourInterface {
...
}
class B implements YourInterface {
...
}
class YourModule extends AbstractModule {
@Override protected void configure() {
Multibinder.newSetBinder(YourInterface.class).addBinding().to(A.class):
Multibinder.newSetBinder(YourInterface.class).addBinding().to(B.class):
}
}
你可以注入(inject) Set<YourInterface>
任何地方:
class SomeClass {
@Inject public SomeClass(Set<YourInterface> allImplementations) {
...
}
}
这应该符合您的需要。
关于java - 如何使用 Guice 获取接口(interface)的所有实现者/子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085200/