java - Why Guice prevents from binding to Provider? -
java - Why Guice prevents from binding to Provider? -
recently, when played around google guice trying this:
@override protected void configure() { ... bind(provider.class).to(viewfactory.class); ... } where viewfactory was:
public class viewfactory implements provider<sometype> {...} of course, guice didn't allow me returing error:
1) binding provider not allowed. {stacktrace} what reason why not possible bind provider?
i guess because provider interface special guice. in fact, internal machinery implemented in term of providers.
moreover, create ambiguities. if bindings providers possible:
bind(someclass.class).to(someclassimpl1.class); bind(new typeliteral<provider<someclass>>() {}).to(() -> new someclassimpl2()); then should guice inject here?
@inject otherclass(provider<someclass> someclassprovider) { ... } should provider returns someclassimpl1 (because of first binding; remember, direct injections , provider injections interchangeable in guice) or should provider returns someclassimpl2 (because of sec binding)?
it redundant. because can inject someclass or provider<someclass> regardless of actual binding, can bind class provider:
bind(someclass.class).toprovider(() -> new someclassimpl()); // either of next work @inject otherclass1(provider<someclass> someclassprovider) { ... } @inject otherclass2(someclass someclass) { ... } java dependency-injection guice
Comments
Post a Comment