我们in(Scopes.SINGLETON)
在 Guice 中有两个单例对象(通过 声明),每个对象在其构造函数中使用另一个。Guice 实现这一点的方法是使用代理 - 它首先使用另一个对象的代理来初始化对象,并且只有在需要该对象时才会解析它。
从多个线程运行此代码时,我们得到以下异常:
java.lang.IllegalStateException: This is a proxy used to support
circular references involving constructors. The object we're proxying is not
constructed yet. Please wait until after injection has completed to use this
object.
at
com.google.inject.internal.ConstructionContext$DelegatingInvocationHandler.invoke(ConstructionContext.java:100)
我认为这是 Guice 中的一个错误,因为我们没有做任何不寻常的事情。我们发现的一种解决方法是提前使用 初始化单例.asEagerSingleton()
,但这不是很方便,例如用于测试。
这是一个已知的问题?报告了Google Guice 的问题,通过独立测试重现。
还有其他建议/解决方法吗?