为什么我们得到一个StackOverflowError来发现@Inject方法?

时间:2015-08-03 22:30:44

标签: java dependency-injection stack-overflow guice

我们遇到了StackOverflowError,因为在Guice中我们有深度嵌套注入。

在callstack中,有一个Guice模式分析一个类Constructor,然后发现一个被注入的新类,并重复这个过程,直到它的16个类深入POP。

我想有一种方法让我可以事先告诉Guice一些需要注入的类。我不确定那个过程会是什么。

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

1 个答案:

答案 0 :(得分:0)

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

它不是递归地解析关系,而是已经知道了大部分关系,堆栈从未超过2或3层。