0

我正在尝试匕首,我当前的用例由下面的代码表示。我认为 Dagger 会递归地注入似乎不会发生的对象。

我在top.inner.

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

我不知道这是否是 Dagger 的期望行为,但如果是的话,你能否建议我可能的方法来做到这一点,而不是诉诸于对每个对象进行注入。

编辑我改变了一点用例。我忘记了一个小细节:(。

谢谢

4

1 回答 1

2

每种类型都可以由注释创建@Provides或具有@Inject注释,但不能两者兼有。您在上面代码中的解决方案是删除@ProvidesBottom.

于 2013-09-04T22:49:05.773 回答