Guice绑定通用对象列表

Jam*_*son 6 java guice

什么是绑定语句我需要告诉guice我想要OneFoo和TwoFoo注入Bar作为Foo列表?这里的设置是一个责任链.现在我有两个实现,但Bar不需要知道.

@Inject
Bar(List<Foo> foos) {...}
...
class OneFoo implements Foo {
    @Inject
    OneFoo(...) {...}
...
class TwoFoo implements Foo {
    @Inject
    TwoFoo(...) {...}
Run Code Online (Sandbox Code Playgroud)

但我正在努力使用Types,TypeLiteral等来配置两个Foo实现将赋予Bar的绑定.

ɲeu*_*urɳ 10

如果您在编译时知道绑定,则可以@Provides在模块中使用方法:

class MyModule extends AbstractModule() {
    @Override
    protected void configure() {
        // ...
    }

    @Provides
    @Inject
    public List<Foo> foos(OneFoo one, TwoFoo two) {
        return Arrays.asList(one, two);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以foos根据需要展开参数列表.类似但更冗长的方法是使用提供者:

protected void configure() {
    bind(new TypeLiteral<List<Foo>>() {})
       .toProvider(FooListProvider.class);
}

static class FooListProvider implements Provider<List<Foo>> {
    @Inject
    Provider<OneFoo> one;
    @Inject
    Provider<TwoFoo> two;

    public List<Foo> get() {
        return Arrays.asList(one.get(), two.get());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要注入OneFoo和TwoFoo的单例列表,可以添加@Singleton注释.我建议在这一点上使列表不可变:

@Singleton
@Provides
@Inject
public List<Foo> foos(OneFoo one, TwoFoo two) {
    return Collections.unmodifiableList(Arrays.asList(one, two));
}
Run Code Online (Sandbox Code Playgroud)

另一方面,如果你想要一个不注入OneFoo和TwoFoo的单例列表,你可以使用TypeLiteral:

@Override
protected void configure() {
    bind(new TypeLiteral<List<Foo>>() {})
        .toInstance(Arrays.asList(new OneFoo(), new TwoFoo()));
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我也建议将列表不可修改.