title: Java Guice: Deep object injection
body: <p>I have classes B, C, and D. Just now, I want to add a class A whose attributes are wholly bound to values in an app config. What's the best practice in using Guice to inject the values into class A?</p>
  
  <pre><code>public class A {
      public A(@Named("id") String id, ..., @Named("attr") String attr) {
          ....
      }   
  }
  
  public class B {
      public B() {
          this.a = new A();
      }
  }
  
  
   public class C {
      public C() {
           this.b = new B();
      }
   }
  
   public class D {
  
       @Inject
       private Injector injector;
  
       public D() {
            this.c = new C();
       }
   }
  </code></pre>
  
  <p>One possibility I can think of is to keep passing Injector instance from D to B and change B s.t.:</p>
  
  <pre><code>public class B {
      public B(Injector injector) {
          this.a = injector.getInstance(A.class);
      }
  }
  </code></pre>
  
  <p>This seems bad form and I have not seen online examples using this. Any suggestions would be welcomed.</p>