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

2020-07-30 03:14发布

问题:

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:

Use this binding:

bind(new TypeLiteral<Class<? extends SuperClass>>() {})
    .toInstance(SubClass.class);