使用Guice注入通用接口子类型的实现

时间:2014-05-22 12:09:25

标签: java generics dependency-injection guice

我定义了以下API:

public interface Input<I> {
    Collection<? extends I> read();
}

public interface Transformer<O, I> {
    Collection<? extends O> transform(Collection<? extends I> inputData);
}

public interface Output<O> {
    void write(Collection<? extends O> output);
}

public interface Executor {
    void execute();
}

以及以下实施:

public final class InputImpl implements Input<String> {
    @Override
    public Collection<? extends String> read() {
        return asList("1", "2");
    }
}

public final class TransformerImpl implements Transformer<Integer, String> {
    @Override
    public Collection<? extends Integer> transform(final Collection<? extends String> inputData) {
        return inputData.stream().map(Integer::parseInt).collect(Collectors.toList());
    }
}

public final class OutputImpl implements Output<Integer> {
    @Override
    public void write(final Collection<? extends Integer> output) {
        System.out.println(output);
    }
}

public final class ExecutorImpl<I, O> implements Executor {
    private final Input<I> input;
    private final Transformer<O, I> transformer;
    private final Output<O> output;

    @Inject
    public ExecutorImpl(final Input<I> input, final Transformer<O, I> transformer, final Output<O> output) {
        this.input = input;
        this.transformer = transformer;
        this.output = output;
    }

    public void execute() {
        final Collection<? extends I> inputData = input.read();
        final Collection<? extends O> outputData = transformer.transform(inputData);
        output.write(outputData);
    }
}

这是我尝试将API绑定到上面的实现:

public final class ModuleImpl extends AbstractModule {
    @Override
    protected void configure() {
        bind(new TypeLiteral<Input<String>>() {
        }).to(InputImpl.class);

        bind(new TypeLiteral<Transformer<Integer, String>>() {
        }).to(TransformerImpl.class);

        bind(new TypeLiteral<Output<Integer>>() {
        }).to(OutputImpl.class);

        bind(Executor.class).to(ExecutorImpl.class);
    }
}

当我尝试使用Guice注入器获取Executor的实例时,我收到以下错误:

1) com.csc.playground.guice.api.Input<I> cannot be used as a key; It is not fully specified.
  at com.csc.playground.guice.ExecutorImpl.<init>(ExecutorImpl.java:25)
  at com.csc.playground.guice.module.ModuleImpl.configure(ModuleImpl.java:33)

2) com.csc.playground.guice.api.Transformer<O, I> cannot be used as a key; It is not fully specified.
  at com.csc.playground.guice.ExecutorImpl.<init>(ExecutorImpl.java:25)
  at com.csc.playground.guice.module.ModuleImpl.configure(ModuleImpl.java:33)

3) com.csc.playground.guice.api.Output<O> cannot be used as a key; It is not fully specified.
  at com.csc.playground.guice.ExecutorImpl.<init>(ExecutorImpl.java:25)
  at com.csc.playground.guice.module.ModuleImpl.configure(ModuleImpl.java:33)

1 个答案:

答案 0 :(得分:3)

从概念上讲,问题在于:

@Inject
public ExecutorImpl(final Input<I> input,
        final Transformer<O, I> transformer,
        final Output<O> output) {

当guice试图履行Input<I>的依赖时,它并不知道它是什么类型;这是什么&#34;它没有完全指定&#34;装置

您可以通过指定泛型类型来解决此问题。您执行此操作的方式与在AbstractModule中执行绑定的方式相同,例如,而不是绑定到ExecutorImpl.class,执行:

bind(Executor.class).to(new TypeLiteral<ExecutorImpl<String, Integer>>() {});