Using Guice, how can I inject a bounded-wildcard c

2020-07-30 03:45发布

Using Guice, I want to inject a bounded-wildcard class. To be clear, I don't want to inject an object, but inject a class type. The would read:

class A {
   Class<? extends SuperClass> a;
   @Inject A(Class<? extends SuperClass> a) {
      this.a = a.;
   }
}

How can I correctly bind the parameter?

1条回答
ゆ 、 Hurt°
2楼-- · 2020-07-30 04:19

Use this binding:

bind(new TypeLiteral<Class<? extends SuperClass>>() {})
    .toInstance(SubClass.class);
查看更多
登录 后发表回答