[英]What's the difference between guice bind to instance and asEagersingleton
When we do a 当我们做一个
bind(ClassName).toInstance(new ClassName())
inside the configure method do we essentially mean that it is an "eagerly initialized singleton" by default? 在configure方法中的bind(ClassName).toInstance(new ClassName())
实质上是否意味着默认情况下它是“急于初始化的单例”?
If yes, what is the use of adding 如果是,添加的用途是什么
bind(ClassName).toInstance(new ClassName()).asEagerSingleton()
.asEagerSingleton()
无法附加.asEagerSingleton()
The complete signature of toInstance
is the following: toInstance
的完整签名如下:
void toInstance(T instance)
Since toInstance(T)
returns nothing, you can't chain it with .asEagerSingleton()
. 由于toInstance(T)
返回任何内容,因此无法将其与.asEagerSingleton()
。 The compilation will fail if you do that. 如果这样做,编译将失败。
As you suspected, toInstance
is already an eagerly-loaded singleton, this is why it's a chain-ending method ( void
) and not a binding declaration that can be further scoped. 正如您所怀疑的那样, toInstance
已经是一个渴望加载的单例,这就是为什么它是一个链终止方法( void
)而不是可以进一步作用域的绑定声明的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.