By default Guice does not require you to annotate your public constructor with @Inject - if there's only one such public constructor then it will automatically pick that one. Even when there are multiple constructors then you can use a constructor binding to tell Guice which one to use and avoid the ambiguity: https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/binder/LinkedBindingBuilder.html#toConstructor-java.lang.reflect.Constructor- Using @Inject is still recommended for code under your control because it documents that class was written with injection in mind. You can also tell Guice to enforce the existence of @Inject on constructors if you wish: https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/Binder.html#requireAtInjectOnConstructors--
Read full article from Can Guice do DI without the @Inject annotation on constructors without real side-effects? · Issue #1079 · google/guice
No comments:
Post a Comment