gpt4 book ai didi

java - 为什么我们在发现 @Inject 方法时会遇到 StackOverflowError ?

转载 作者:行者123 更新时间:2023-11-30 08:01:52 25 4
gpt4 key购买 nike

我们遇到了 StackOverflowError,因为在 Guice 中我们有深层嵌套注入(inject)。

查看调用堆栈,有一种 Guice 分析类构造函数的模式,然后发现注入(inject)的新类,并重复此过程直到 16 个类深度和 POP。

我想有一种方法可以让我事先向 Guice 指示一些需要注入(inject)的类。我不确定这个过程是什么。

com.google.common.util.concurrent.ExecutionError: 
com.google.common.util.concurrent.ExecutionError:
..... x16
com.google.common.util.concurrent.ExecutionError:
com.google.common.util.concurrent.ExecutionError:
java.lang.StackOverflowError


at com.google.common.cache.LocalCache$Segment.get(SourceFile:2199)
at com.google.common.cache.LocalCache.get(SourceFile:3934)
at com.google.common.cache.LocalCache.getOrLoad(SourceFile:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(SourceFile:4821)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(SourceFile:4827)
at com.google.inject.internal.FailableCache.get(SourceFile:48)
at com.google.inject.internal.ConstructorInjectorStore.get(SourceFile:50)
at com.google.inject.internal.ConstructorBindingImpl.initialize(SourceFile:136)
at com.google.inject.internal.InjectorImpl.initializeJitBinding(SourceFile:547)
at com.google.inject.internal.InjectorImpl.createJustInTimeBinding(SourceFile:884)
at com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(SourceFile:805)
at com.google.inject.internal.InjectorImpl.getJustInTimeBinding(SourceFile:282)
at com.google.inject.internal.InjectorImpl.getBindingOrThrow(SourceFile:214)
at com.google.inject.internal.InjectorImpl.getInternalFactory(SourceFile:890)
at com.google.inject.internal.FactoryProxy.notify(SourceFile:46)
at com.google.inject.internal.ProcessedBindingData.runCreationListeners(SourceFile:50)
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(SourceFile:134)
at com.google.inject.internal.InternalInjectorCreator.build(SourceFile:107)
at com.google.inject.Guice.createInjector(SourceFile:96)
at com.google.inject.Guice.createInjector(SourceFile:73)


Caused by: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: java.lang.StackOverflowError
at com.google.common.cache.LocalCache$Segment.get(SourceFile:2199)
at com.google.common.cache.LocalCache.get(SourceFile:3934)
at com.google.common.cache.LocalCache.getOrLoad(SourceFile:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(SourceFile:4821)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(SourceFile:4827)
at com.google.inject.internal.FailableCache.get(SourceFile:48)
at com.google.inject.internal.MembersInjectorStore.get(SourceFile:68)
at com.google.inject.internal.ConstructorInjectorStore.createConstructor(SourceFile:74)
at com.google.inject.internal.ConstructorInjectorStore.access$000(SourceFile:29)
at com.google.inject.internal.ConstructorInjectorStore$1.create(SourceFile:37)
at com.google.inject.internal.ConstructorInjectorStore$1.create(SourceFile:33)
at com.google.inject.internal.FailableCache$1.load(SourceFile:37)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(SourceFile:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(SourceFile:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(SourceFile:2280)
at com.google.common.cache.LocalCache$Segment.get(SourceFile:2195)
... 35 more

依此类推,进行 x16 回避,直到出现 StackOverflowError

最佳答案

将bind(Class clazz)添加到模块的configure()方法中就可以了。

它不是递归地解决关系,而是已经知道其中的大多数关系,并且堆栈的深度从未超过 2 或 3 层。

关于java - 为什么我们在发现 @Inject 方法时会遇到 StackOverflowError ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31797806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com