Guice inject single instance into multiple objects without using @Singleton

后端 未结 1 785
暖寄归人
暖寄归人 2021-01-13 06:33

I was reading the Guice documentation, and came across a section labeled Eliminate the Cycle (Recommended) which peaked my interest because it\'s exactly the issue that led

相关标签:
1条回答
  • 2021-01-13 07:08

    You should use a provider

    public class StoreProvider implements Provider<Store> {
      @Inject 
      private Boss boss ;
    
      public Store get() {
        return new Store(boss, boss.getClerk().getCustomerLine());
      }
    }
    

    And then bind it in your module

    bind(Store.class).toProvider(StoreProvider.class);

    0 讨论(0)
提交回复
热议问题