Q:

Java Guice: Deep object injection

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?
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();
     }
 }

One possibility I can think of is to keep passing Injector instance from D to B and change B s.t.:
public class B {
    public B(Injector injector) {
        this.a = injector.getInstance(A.class);
    }
}

This seems bad form and I have not seen online examples using this. Any suggestions would be welcomed.

A:

rather than new'ing A, B, and C, why not require them to be passed in as dependencies. something like this:
class A {
    private final String id;
    private final String attr;

    @Inject
    A(String id, String attr) {
        this.id   = id;
        this.attr = attr;
    }
}

class B {
    private final A a;

    @Inject
    B(A a) {
        this.a = a;
    }
}

class C {
    private final B b;

    @Inject
    C(B b) {
        this.b = b;
    }
}

class D {
    private final C c;

    @Inject
    D(C c) {
        this.c = c;
    }
}

and then use Guice to create and inject the instances as needed:
@Module
public class Module {
    @Provides
    public A provideA(@Named("id") String id, @Named("attr") String attr) {
        return A(id, attr);
    }

    @Provides 
    public B provideB(A a) {
        return new B(a);
    }

    @Provides 
    public C provideC(B b) {
        return new C(b);
    }

    @Provides 
    public D provideD(C c) {
        return new D(c);
    }
}

