2013-09-04 4 views
0

대거를 사용하려고하고 있으며 현재 사용 사례가 아래 코드로 표시됩니다. 대거를 강요하면 재귀 적으로 객체가 주입되는 것처럼 보입니다.대거 반복 주입

top.inner에 null이 표시됩니다. 이 단검의 원하는 동작 인 경우

public static class Bottom { 

} 

/* 
public static class Inner { 
    @Inject Bottom bottom; 
} 
*/ 

public static interface Inner { 
    public Bottom bottom(); 

    public static class Implementation() { 
     @Inject Bottom bottom; 
     @Override public Bottom bottom() { 
      return bottom; 
     } 
    } 
} 

public static class Top { 
    @Inject Provider<Inner> inner; 
} 

@Module(injects = {DaggerTest.class, /* Top.class, Inner.class, */ Bottom.class}) 
public class AppModule { 
    @Provides public Bottom providesBottom() { 
    return new Bottom(); 
    } 
} 

@Inject Top top; 

@Test 
public void testProviderInjection() { 
    ObjectGraph graph = ObjectGraph.create(new AppModule()); 
    graph.inject(this); 

    Assert.assertNotNull("top", top); 
    Assert.assertNotNull("top.inner", top.inner); 
    Assert.assertNotNull("top.inner.get()", top.inner.get()); 
    /* Assert.assertNotNull("top.inner.get().bottom", top.inner.get().bottom); */ 
    Assert.assertNotNull("top.inner.get().bottom", top.inner.get().bottom()); 
} 

나는 더 ideia이 없다하지만이 경우 당신은 내게 모든 객체에 주입에 의지 whitout이 작업을 수행 할 수있는 가능한 방법을 제안 할 수 있습니다.

수정 조금 변경된 유스 케이스입니다. 나는 약간의 세부 사항 :(잊어 버려.

감사

답변

2

각 유형은 @Provides 만들거나 @Inject 주석을 가지고 있지만, 그것은 모두를 가질 수 없습니다. 위의 코드에있는 당신의 솔루션에 대한 @Provides 방법을 삭제하는 것입니다 수 있습니다 모든 것 외에도 Bottom

+0

응답 해 주셔서 감사합니다. Jesse,하지만 조금 자세하게 설명 드리지만, Inner는 인터페이스입니다. – fampinheiro

+0

Inner에 대한 @Provides를 정의하려면 어떻게합니까? 내부적으로 구현되어 구현 된 인터페이스를 반환하도록 선택합니다. –

관련 문제