java-使用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)

解决方法:

从概念上讲,问题出在这里:

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

当guice尝试满足Input< I>的依赖关系时,它不知道它是什么类型.这就是“未完全指定”的意思.

您可以通过指定通用类型来解决此问题.您可以按照在AbstractModule中进行绑定的方式来执行此操作,例如,不要绑定到ExecutorImpl.class,而是:

bind(Executor.class).to(new TypeLiteral<ExecutorImpl<String, Integer>>() {});
上一篇:Java-Guice vs EJB,用于注入池


下一篇:java-Guice嵌套通用类型绑定