Java 对不完整父级的Dagger.plus()进行回溯

Java 对不完整父级的Dagger.plus()进行回溯,java,dependency-injection,dagger,Java,Dependency Injection,Dagger,考虑匕首模块: @Module(library = true, complete = false) public static class Module { @Provides public Contextualized providesContextualized(Context ctx) { return new Contextualized(ctx.getUsername()); } // ... and many more such pro

考虑匕首模块:

@Module(library = true, complete = false)
public static class Module {
    @Provides
    public Contextualized providesContextualized(Context ctx) {
        return new Contextualized(ctx.getUsername());
    }
    // ... and many more such provides.
}
上下文是可以连接到例如HTTP会话的对象 这在启动时是不可能知道的,通常在启动时会创建一个 图表:

考虑到该模块足够长,它似乎可以 首先为模块创建一个图形:

ObjectGraph baseline = ObjectGraph.create(new Module());
然后,在处理特定请求时,创建一个 使图形完整的图形:

ObjectGraph withContext = baseline.plus(new ContextModule("killroy"));
java.lang.IllegalStateException: Errors creating object graph:
Context could not be bound with key Context required by class PlusExample$Module
at dagger.internal.ThrowingErrorHandler.handleErrors(ThrowingErrorHandler.java:34)
at dagger.internal.Linker.linkRequested(Linker.java:182)
at dagger.internal.Linker.linkAll(Linker.java:109)
at dagger.ObjectGraph$DaggerObjectGraph.linkEverything(ObjectGraph.java:244)
at dagger.ObjectGraph$DaggerObjectGraph.plus(ObjectGraph.java:203)
at PlusExample.plusFailsOnIncompleteModule(PlusExample.java:46)
但是,.plus()似乎假定继承的图是完整的:

ObjectGraph withContext = baseline.plus(new ContextModule("killroy"));
java.lang.IllegalStateException: Errors creating object graph:
Context could not be bound with key Context required by class PlusExample$Module
at dagger.internal.ThrowingErrorHandler.handleErrors(ThrowingErrorHandler.java:34)
at dagger.internal.Linker.linkRequested(Linker.java:182)
at dagger.internal.Linker.linkAll(Linker.java:109)
at dagger.ObjectGraph$DaggerObjectGraph.linkEverything(ObjectGraph.java:244)
at dagger.ObjectGraph$DaggerObjectGraph.plus(ObjectGraph.java:203)
at PlusExample.plusFailsOnIncompleteModule(PlusExample.java:46)
我是否误解了.plus()的作用,或者这是一个限制 匕首?有没有其他直接的方法来介绍 用户进入图表较晚?(如果每个人都提供 in模块必须从threadlocal或
一些类似的问题。)

看起来像是对您问题的回答:

希望有帮助