Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency Injection with Guice: Something that isn't covered by any tutorial
    primarykey
    data
    text
    <p>i just tinkered around with <a href="http://code.google.com/p/google-guice/" rel="noreferrer">Google Guice</a> for Dependency Injection and started integrating it into my existing application. So far, so good. I have many classes which need, beside their dependencies, Strings, DataSources, et cetera. I know there are NamedBindings, but i really do not want to create an annotation for every simple String i have to pass to the constructor for each class. Then, there is a thing called AssistedInject, creating Factory implementions for me. Wow, but i still have to define the interface of the factory. Thats okay for classes which DO HAVE dependencies, but what about this example class:</p> <pre><code>public class FooBarClass { public FooBarClass(String name, String anotherOne) { // Some stuff } } </code></pre> <p>There are cases where i am in doubt how to use Guice or, more generally, DI the right way. "Often i hear: XYZ Framework is the new <strong>new</strong>." But this implicit that i have to create <strong>every</strong> instance with the DI framework.</p> <h1>Only one instance is required</h1> <p>What if i need only one instance of this class? This class has absolutly no dependencies beside two Strings. Think about a Shutdown Hook which will be instanciated only once and passed to the JVM as my Shutdown Hook. Should i create this instance with Guice? This looks very dumb to me, because there is nothing to inject, but i have to write a factory interface to pass Guide both parameters and have to create an interface for my FooBarClass to use DI.</p> <h1>Multiple instances are required</h1> <p>The same thing applies to a case where i need multiple instances of this class. No dependencies, but i have to create a bunch of boilerplate code to get nothing out of it. This seems wrong to me.</p> <p>So, how i am supposed to use DI and/or Guice?</p> <p>Thanks a lot!</p>
    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. 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