I have seen this post about registering generic type.
example on how to register:
bind(new TypeLiteral<Dal<RoutingResponse>>() {}).to((Class<? extends Dal<RoutingResponse>>) ResponseDal.class);
however how can I get an instance of a generic type from the injector?
I have tried:
injector.getInstance(Dal<RoutingResponse>().getClass());
but got compilation error.
How should I write this?
You can use Key to get instances of these bindings:
injector.getInstance(new Key<Dal<RoutingResponse>>() {}); // supplied by @DanielPryden in the comments
or in a longer version, with a TypeLiteral:
injector.getInstance(Key.get(new TypeLiteral<Dal<RoutingResponse>>() {}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With