假设我有一个接口Foo
。它由具体实现CompositeFoo
,FooA
,FooB
和FooC
实现。此外,CompositeFoo
如下所示:
public class CompositeFoo implements Foo {
@Inject public CompositeFoo(List<? extends Foo> elements);
}
我希望在Guice
PrivateModule
中将Foo
绑定到CompositeFoo
,列表是FooA
,后跟FooB
或FooC
。 (这必须是列表,因为顺序很重要;这排除了多重绑定作为解决方案。)问题是我看到涉及一些循环性。假设
CompositeFoo
的提供者如下:public class CompositeFooProvider implements Provider<Foo> {
@Inject private FooA first;
@Inject @Named("Inner") private Foo second;
@Override public Foo get() { return new CompositeFoo(asList(first, second)); }
}
提供第二个
Foo
(FooB
或FooC
)的模块如下:public class InnerModule extends PrivateModule {
private final Key<? super Foo> bindingKey; // key will be exposed, bound to the Foo below
// configure() deals with deps of FooB and FooC
@Provides
public Foo getInnerFoo(...) {
// Assume that the args are such that if they are "valid", we should return a FooB, else FooC
if (...) return new FooB(...);
else return new FooC(...);
}
}
当我尝试构造外部模块时会出现这种圆形性:我需要安装
InnerModule
(将Key.get(Foo.class, Names.named("Inner"))
作为绑定键传递)以获得第二个Foo
,但是Foo
已经绑定到了外部模块,因为它绑定到CompositeFooProvider
。如何解决这种循环性?将@Provides
方法转换为自己的Provider
就足够了吗? 最佳答案
@Provides Foo
方法为Foo
提供绑定,该绑定与外部模块中的Foo
绑定冲突。因此,将其绑定为其他内容:
public class InnerModule extends PrivateModule {
private final Key<Foo> bindingKey; // key will be exposed, bound to the @Inner Foo below
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
@interface Inner {
}
@Override
protected void configure() {
bind(bindingKey).to(Key.get(Foo.class, Inner.class));
expose(bindingKey);
}
@Provides
@Inner Foo getInnerFoo(...) {
// Assume that the args are such that if they are "valid", we should return a FooB, else FooC
if (...) return new FooB(...);
else return new FooC(...);
}
}
或者也许你可以做
@Provides
@Exposed
@Named("Inner") Foo getInnerFoo(...) {
// Assume that the args are such that if they are "valid", we should return a FooB, else FooC
if (...) return new FooB(...);
else return new FooC(...);
}
直接并且不必费心传递绑定密钥。