I am trying to inject a Map into a class using Guice where the map has the form Map<MyInterface, Integer>
.
I want to use the MapBinder
extention to accomplish this, but it seems that MapBinder requires an instantiated object for the key. I would like to have Guice inject instantiations of the key, since they are complex objects that require injections of their own. I.e, something like:
MapBinder<MyInterface, Integer> mapBinder =
MapBinder.newMapBinder(binder(), MyInterface.class, Integer.class);
mapBinder.addBinding(MyInterfaceImpl1.class).to(5);
mapBinder.addBinding(MyInterfaceImpl2.class).to(6);
This is illegal though, since addBinding expects a instance of the class.
I know I could switch the order of the objects in the map, but the integer values are not unique so then I'd end up with a Map of Integer -> List, which is rather ugly. Anyone have any ideas?