【问题标题】:What's the difference between guice bind to instance and asEagersingletonguice 绑定到实例和 asEagersingleton 有什么区别
【发布时间】:2019-04-02 20:56:03
【问题描述】:

当我们做一个

bind(ClassName).toInstance(new ClassName()) 在 configure 方法中,我们本质上是说它是默认的“急切初始化的单例”吗?

如果是,添加有什么用

bind(ClassName).toInstance(new ClassName()).asEagerSingleton()

【问题讨论】:

    标签: java dependency-injection guice


    【解决方案1】:

    无法追加.asEagerSingleton()

    complete signature of toInstance 如下:

    void toInstance(T instance)
    

    由于toInstance(T) 不返回任何内容,因此您不能将其与.asEagerSingleton() 链接。如果这样做,编译将失败。

    正如您所怀疑的,toInstance 已经是一个热切加载的单例,这就是为什么它是一个链式结束方法 (void) 而不是可以进一步限定范围的绑定声明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多