Does Guice binding bind subclass as well? -
so have module binds interface implementation class.
bind(ilocalstore.class).to(localstore.class);
will binding inject following constructor:
@inject public localstoreuser(localstore localstore) { this.localstore = localstore }
the injection work, not because of binding.
guice treats lookups if simple map<key, provider>
lookup, key composed of (binding annotation, parameterized type) pair. guice not automatically walk type hierarchy you, or otherwise modify lookup key (checking non-annotated or raw type binding, instance).
however, if localstore has public no-argument constructor, guice can create type directly, code above work—it has nothing binding subclasses automatically.
consider following example:
interface {} interface b extends {} public class c implements b {}
without bindings @ all, inject c, because has implicit public no-arg constructor. requesting injection of or b fail.
if
bind(a.class).to(c.class)
inject (explicit) or c (implicit) not b. guice not bind subinterface well, b has no implementation.same goes superclasses/superinterfaces: if
bind(b.class).to(c.class)
inject b (explicit) or c (implicit) not a, though b extends a.if
bind(a.class).to(b.class)
andbind(b.class).to(c.class)
, inject a, b, or c without repeating yourself.
Comments
Post a Comment