Skip to content

Instantly share code, notes, and snippets.

@coderunner
Created August 12, 2011 03:01
Show Gist options
  • Select an option

  • Save coderunner/1141345 to your computer and use it in GitHub Desktop.

Select an option

Save coderunner/1141345 to your computer and use it in GitHub Desktop.

Revisions

  1. coderunner revised this gist Aug 12, 2011. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,8 @@ public void initInject()
    {
    //setup injection bindings, this is done once at initialization
    Injector.Builder builder = new Injector.Builder();
    injector = builder.addClassBinding(AnotherDependencyInterface.class, AnotherDependency.class)
    injector = builder
    .addClassBinding(AnotherDependencyInterface.class, AnotherDependency.class)
    .addClassBinding(DependencyInterface.class, Dependency.class)
    .addClassBinding(ExampleClass.class, ExampleClass.class)
    .build();
  2. coderunner revised this gist Aug 12, 2011. 1 changed file with 42 additions and 45 deletions.
    87 changes: 42 additions & 45 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -2,55 +2,52 @@

    public class MainClass
    {
    private Injector injector;
    private Injector injector;

    /** MANUAL OBJECT TREE SETUP **/
    public void manualObjectTreeSetup()
    {
    //create the dependency tree
    //this can get really annoying
    AnotherDependencyInterface anotherDependency =
    new AnotherDependency();
    DependencyInterface dependency =
    new Dependency(anotherDependency);
    //create the object
    ExampleClass object = new ExampleClass(dependency);
    }
    /** MANUAL OBJECT TREE SETUP **/
    public void manualObjectTreeSetup()
    {
    //create the dependency tree
    //this can get really annoying
    AnotherDependencyInterface anotherDependency =
    new AnotherDependency();

    DependencyInterface dependency =
    new Dependency(anotherDependency);

    //create the object
    ExampleClass object = new ExampleClass(dependency);
    }

    /** USING DEPENDENCY INJECTION **/
    public void initInject()
    {
    //setup injection bindings, this is done once at initialization
    Injector.Builder builder = new Injector.Builder();
    injector = builder.addClassBinding(AnotherDependencyInterface.class,
    AnotherDependency.class)
    .addClassBinding(DependencyInterface.class,
    Dependency.class)
    .addClassBinding(ExampleClass.class,
    ExampleClass.class)
    .build();
    }
    /** USING DEPENDENCY INJECTION **/
    public void initInject()
    {
    //setup injection bindings, this is done once at initialization
    Injector.Builder builder = new Injector.Builder();
    injector = builder.addClassBinding(AnotherDependencyInterface.class, AnotherDependency.class)
    .addClassBinding(DependencyInterface.class, Dependency.class)
    .addClassBinding(ExampleClass.class, ExampleClass.class)
    .build();
    }

    public void injectObjectTreeSetup()
    {
    //create the required object
    //the injector will create the require dependencies and inject
    //them in the newly create instance
    ExampleClass object = injector.createObject(ExampleClass.class);
    }
    public void injectObjectTreeSetup()
    {
    //create the required object
    //the injector will create the require dependencies and inject
    //them in the newly create instance
    ExampleClass object = injector.createObject(ExampleClass.class);
    }

    /** MAIN **/
    public static void main(String args[])
    {
    MainClass main = new MainClass();
    /** MAIN **/
    public static void main(String args[])
    {
    MainClass main = new MainClass();

    //manual setup
    main.manualObjectTreeSetup();
    //manual setup
    main.manualObjectTreeSetup();

    //using DI library
    main.initInject();
    main.injectObjectTreeSetup();
    }
    //using DI library
    main.initInject();
    main.injectObjectTreeSetup();
    }
    }
  3. coderunner revised this gist Aug 12, 2011. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    import org.inject.Injector;

    public class MainClass
    {
    private Injector injector;

    /** MANUAL OBJECT TREE SETUP **/
    public void manualObjectTreeSetup()
    {
    //create the dependency tree
    @@ -16,7 +19,8 @@ public void manualObjectTreeSetup()
    ExampleClass object = new ExampleClass(dependency);
    }

    public void init()
    /** USING DEPENDENCY INJECTION **/
    public void initInject()
    {
    //setup injection bindings, this is done once at initialization
    Injector.Builder builder = new Injector.Builder();
    @@ -37,6 +41,7 @@ public void injectObjectTreeSetup()
    ExampleClass object = injector.createObject(ExampleClass.class);
    }

    /** MAIN **/
    public static void main(String args[])
    {
    MainClass main = new MainClass();
    @@ -45,7 +50,7 @@ public static void main(String args[])
    main.manualObjectTreeSetup();

    //using DI library
    main.init();
    main.initInject();
    main.injectObjectTreeSetup();
    }
    }
  4. coderunner created this gist Aug 12, 2011.
    51 changes: 51 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    public class MainClass
    {
    private Injector injector;

    public void manualObjectTreeSetup()
    {
    //create the dependency tree
    //this can get really annoying
    AnotherDependencyInterface anotherDependency =
    new AnotherDependency();

    DependencyInterface dependency =
    new Dependency(anotherDependency);

    //create the object
    ExampleClass object = new ExampleClass(dependency);
    }

    public void init()
    {
    //setup injection bindings, this is done once at initialization
    Injector.Builder builder = new Injector.Builder();
    injector = builder.addClassBinding(AnotherDependencyInterface.class,
    AnotherDependency.class)
    .addClassBinding(DependencyInterface.class,
    Dependency.class)
    .addClassBinding(ExampleClass.class,
    ExampleClass.class)
    .build();
    }

    public void injectObjectTreeSetup()
    {
    //create the required object
    //the injector will create the require dependencies and inject
    //them in the newly create instance
    ExampleClass object = injector.createObject(ExampleClass.class);
    }

    public static void main(String args[])
    {
    MainClass main = new MainClass();

    //manual setup
    main.manualObjectTreeSetup();

    //using DI library
    main.init();
    main.injectObjectTreeSetup();
    }
    }