假设我有一个接口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
是否足够?
答案 0 :(得分:1)
@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(...);
}
直接而不是打扰传递绑定密钥。