【发布时间】:2019-04-02 20:56:03
【问题描述】:
当我们做一个
bind(ClassName).toInstance(new ClassName())
在 configure 方法中,我们本质上是说它是默认的“急切初始化的单例”吗?
如果是,添加有什么用
bind(ClassName).toInstance(new ClassName()).asEagerSingleton()
【问题讨论】:
标签: java dependency-injection guice
当我们做一个
bind(ClassName).toInstance(new ClassName())
在 configure 方法中,我们本质上是说它是默认的“急切初始化的单例”吗?
如果是,添加有什么用
bind(ClassName).toInstance(new ClassName()).asEagerSingleton()
【问题讨论】:
标签: java dependency-injection guice
.asEagerSingleton()
complete signature of toInstance 如下:
void toInstance(T instance)
由于toInstance(T) 不返回任何内容,因此您不能将其与.asEagerSingleton() 链接。如果这样做,编译将失败。
正如您所怀疑的,toInstance 已经是一个热切加载的单例,这就是为什么它是一个链式结束方法 (void) 而不是可以进一步限定范围的绑定声明。
【讨论】: