如何让Guice正确绑定此泛型?

时间:2018-06-11 20:12:05

标签: java guice

我正在开发一个处理消息的平台,我遇到了Guice绑定的问题。我写了一个示例项目,显示我遇到的问题,这是一个例外情况,如下所示:

1) com.icacarealign.worker_example.PreprocessorMiddleware<MessageType> cannot be used as a key; It is not fully specified.

这是代码。

接口

public interface Middleware {
  void configure();
}

public interface PreprocessorMiddleware<MessageType> extends Middleware {
  void prework(Context<MessageType> contextObj);
}

public interface PostprocessorMiddleware<MessageType> extends Middleware {
  void postwork(Context<MessageType> contextObj);
}

Guice Binding

public class MiddlewareModule<MessageType> extends AbstractModule {
  private final List<Class<? extends PostprocessorMiddleware<MessageType>>> _postprocessors;
  private final List<Class<? extends PreprocessorMiddleware<MessageType>>> _preprocessors;

  public MiddlewareModule(
    List<Class<? extends PreprocessorMiddleware<MessageType>>> preprocessors,
    List<Class<? extends PostprocessorMiddleware<MessageType>>> postprocessors
  ) {
    _preprocessors = preprocessors;
    _postprocessors = postprocessors;
  }

  @Override
  protected void configure() {
    bindAllMiddleware(_preprocessors, new TypeLiteral<PreprocessorMiddleware<MessageType>>() {});
    bindAllMiddleware(_postprocessors, new TypeLiteral<PostprocessorMiddleware<MessageType>>() {});
  }

  private <T extends Middleware> void bindAllMiddleware(List<Class<? extends T>> middleware, TypeLiteral<T> type) {
    Multibinder<T> multibinder = Multibinder.newSetBinder(binder(), type);

    middleware.forEach(middlewareType -> bindMiddleware(multibinder, middlewareType));
  }

  private <T extends Middleware> void bindMiddleware(Multibinder<T> binder, Class<? extends T> type) {
    binder().bind(type).in(Singleton.class);
    binder.addBinding().to(type);
  }
}

主要方法

public class Main {
  public static void main(String[] args) {
    List<Class<? extends PreprocessorMiddleware<Message>>> preprocessorMiddlewares = new ArrayList<>();
    List<Class<? extends PostprocessorMiddleware<Message>>> postprocessorMiddlewares = new ArrayList<>();

    preprocessorMiddlewares.add(ArbitraryPrepreprocessorMiddleware.class);
    postprocessorMiddlewares.add(ArbitraryPostprocessorMiddleware.class);

    MiddlewareModule<Message> module = new MiddlewareModule<>(preprocessorMiddlewares, postprocessorMiddlewares);

    Injector injector = Guice.createInjector(module);
  }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

正如jacobm所描述的,你需要解决泛型问题,并且可能需要传入你绑定的类型。幸运的是,您可以使用Guice的Types.newParameterizedType来创建完全指定的TypeLiteral。

public class MiddlewareModule<MessageType> extends AbstractModule {
  private final Class<MessageType> _clazz;
  private final List<Class<? extends PostprocessorMiddleware<MessageType>>> _postprocessors;
  private final List<Class<? extends PreprocessorMiddleware<MessageType>>> _preprocessors;

  public MiddlewareModule(
    // Accept the message type in a way that survives erasure.
    Class<MessageType> clazz,
    List<Class<? extends PreprocessorMiddleware<MessageType>>> preprocessors,
    List<Class<? extends PostprocessorMiddleware<MessageType>>> postprocessors
  ) {
    _clazz = clazz;
    _preprocessors = preprocessors;
    _postprocessors = postprocessors;
  }

  @Override
  protected void configure() {
    // Use the Class to create your fully-specified TypeLiteral.
    bindAllMiddleware(_preprocessors,
        Types.newParameterizedType(PreprocessorMiddleware.class, _clazz));
    bindAllMiddleware(_postprocessors,
        Types.newParameterizedType(PostprocessorMiddleware.class, _clazz));
  }

  private <T extends Middleware> void bindAllMiddleware(List<Class<? extends T>> middleware, TypeLiteral<T> type) {
    Multibinder<T> multibinder = Multibinder.newSetBinder(binder(), type);

    middleware.forEach(middlewareType -> bindMiddleware(multibinder, middlewareType));
  }

  private <T extends Middleware> void bindMiddleware(Multibinder<T> binder, Class<? extends T> type) {
    bind(type).in(Singleton.class);  // Don't call binder() explicitly.
    binder.addBinding().to(type);
  }
}

请注意,这是未经测试的代码,因为我没有完整的SSCCE;你可能需要调整类型参数,以便说服Guice你在泛型中使用通配符是安全的。

答案 1 :(得分:0)

您无法在TypeLiteral中使用类型变量(在本例中为TypeToken)来实现Guice绑定。相反,您需要重构以传递在不使用任何类型变量的情况下构造的WITH cte as ( SELECT *, CASE WHEN [At Loc1] IS NOT NULL THEN row_number() over (order by [At Loc1] ) END as r1, CASE WHEN [At Loc2] IS NOT NULL THEN row_number() over (order by [At Loc2] ) END as r2, CASE WHEN [At Loc3] IS NOT NULL THEN row_number() over (order by [At Loc3] ) END as r3, CASE WHEN [At Loc4] IS NOT NULL THEN row_number() over (order by [At Loc4] ) END as r4 FROM Table1 ) SELECT *, COALESCE(r1,r2,r3,r4) FROM cte ORDER BY COALESCE(r1,r2,r3,r4), COALESCE(r2,r3,r4), COALESCE(r3,r4), r4 ; (可能这意味着将其从调用点传入)。

对于类似情况,

Here是一个更为复杂的答案。结果是,如果你真的需要避免在每个调用点上创建一个类型文字,你可以抽象 little 更多,但它的复杂性非常高。