Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Pass the injector to all subclasses and then use injector.getInstance(...) to create the subclasses?</p> </blockquote> <p>no, by doing this you are defeating the purpose of the <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="noreferrer">dependency injection</a> pattern and also coupling all your implementation to Guice. Your implementations should not interact at all with guice, except through the (now standardized) annotations.</p> <blockquote> <p>Instanciate a new injector like</p> <pre><code>TestConfiguration config = new TestConfiguration(); Injector injector = Guice.createInjector(config); TestImpl test = injector.getInstance(TestImpl.class); </code></pre> <p>in all nested classes?</p> </blockquote> <p>no, and this is even worse cause you will end up with multiple injectors, hence multiple contexts which will prevent a proper usage of the <a href="http://code.google.com/p/google-guice/wiki/Scopes" rel="noreferrer">scopes</a>.</p> <p>Ideally, you should only use the injector during the bootstrapping of your application. Of course the way to bootstrap it will largely depend of the application.</p> <blockquote> <p>Is there an other approach to make the properties available in all classes?</p> </blockquote> <p>The properties could be injected the same way you did for TestImpl. If you want TestImpl to use let say a service which also needs some properties (or other services), just let Guice inject it to TestImpl. Guice is taking care of all the instantiation/wiring. You should only tell Guice "how to proceed", by using the binder, when Guice cannot figure this out itself :</p> <pre><code>public class TestImpl { private final String property1; private final Integer property2; private final IService service; @Inject public TestImpl(@Named("Property1") String property1, @Named("Property2") Integer property2, IService service) { this.property1 = property1; this.property2 = property2; this.service= service; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload