I am trying to migrate a small project, replacing some factories with Guice (it is my first Guice trial). However, I am stuck when trying to inject generics. I managed to ex
I think the specific issue you're seeing is probably because of the bind(Console.class)
statement. It should use a TypeLiteral
as well. Or, you could just bind neither of those and JIT bindings will take care of it for you since both of the types involved here are concrete classes.
Additionally, you should retrieve the Console
with:
Console<Double> cons =
injector.getInstance(Key.get(new TypeLiteral<Console<Double>>(){}));
Edit: You don't need to bind to an instance just because you're using a TypeLiteral
. You can just do:
bind(new TypeLiteral<Console<Double>>(){});
Of course, like I said above you could just skip that in this case and retrieve the Console
from the injector using a Key
based on the TypeLiteral
and the binding would be implicit.