用Guice注入复合材料

时间:2014-11-14 21:41:36

标签: java guice composite

假设我有一个接口Foo。它由具体实现CompositeFooFooAFooBFooC实现。此外,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)); }
}

提供第二个FooFooBFooC)的模块如下:

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是否足够?

1 个答案:

答案 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(...);
    }

直接而不是打扰传递绑定密钥。