Guice在不使用@Singleton的情况下将单个实例注入多个对象 [英] Guice inject single instance into multiple objects without using @Singleton

查看:207
本文介绍了Guice在不使用@Singleton的情况下将单个实例注入多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Guice文档,并看到标有消除循环(推荐),这引起了我的兴趣,因为它正是导致我今天获得文档的问题。

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 me to the documentation today.

基本上,消除循环依赖关系,您将依赖关系案例提取到一个单独的类中。 好的,没有新的东西。

Basically, to eliminate cyclic dependencies, you "extract the Dependency Case into a separate class." Okay, nothing new there.

所以,在这个例子中,我们有。

So, in the example, we have.

public class Store {
        private final Boss boss;
        private final CustomerLine line;
        //...

        @Inject public Store(Boss boss, CustomerLine line) {
                this.boss = boss; 
                this.line = line;
                //...
        }

        public void incomingCustomer(Customer customer) { line.add(customer); } 
}

public class Boss {
        private final Clerk clerk;
        @Inject public Boss(Clerk clerk) {
                this.clerk = clerk;
        }
}

public class Clerk {
        private final CustomerLine line;

        @Inject Clerk(CustomerLine line) {
                this.line = line;
        }

        void doSale() {
                Customer sucker = line.getNextCustomer();
                //...
        }
}

你有一个商店和一个 Clerk ,每个人都需要引用一个 CustomerLine的实例。这个概念没有问题,并且很容易使用经典的依赖注入:

You have a Store and a Clerk and each need to have a reference to a single instance of CustomerLine. No problems with this concept, and easily doable with classic Dependency Injection:

CustomerLine customerLine = new CustomerLine();
Clerk clerk = new Clerk(customerLine);
Boss boss = new Boss(clerk);
Store store = new Store(boss, customerLine);

这很容易,但现在,我需要使用Guice注入来完成此操作。因此,我的问题是实现以下内容:

That's easy enough, but now, I need to do this with Guice injection. Thus, my issue is with implementing the following:


您可能希望确保商店和文员都使用
相同的CustomerLine实例。

you may want to make sure that the Store and Clerk both use the same CustomerLine instance.

是的,这正是我想要做的。 但我如何在Guice模块中执行此操作?

Yes, that is exactly what I want to do. But how do I do that in a Guice module?

public class MyModule extends AbstractModule implements Module {
    @Override
    protected void configure() {
        //Side Question: Do I need to do this if there if Boss.class is the implementation?
        bind(Boss.class);
        bind(CustomerLine.class).to(DefaultCustomerLine.class); //impl
    }
}

我用我的模块创建了一个注入器:

I create an injector with my module:

Injector injector = Guice.createInjector(new MyModule());

现在,我想要一个商店的实例:

Now, I want an instance of Store:

Store store = injector.getInstance(Store.class);

这将注入一个新的 CustomerLine实例 Boss 进入商店的此实例。然而, Boss 获取 Clerk 的实例,该实例还会注入 CustomerLine <的实例/ code>。此时,它将是一个新实例,从注入 Store 的实例中唯一。

This will inject a new instance of CustomerLine and Boss into this instance of Store. Boss, however, gets an instance of Clerk which also gets injected an instance of CustomerLine. At this point, it would be a new instance, unique from the instance injected into Store.


  • 如何商店商员按此顺序共享同一个实例,
    不使用 @Singleton

  • How can Store and Clerk share the same instance in this sequence, without using @Singleton?

请告诉我是否需要更多信息,或者这个问题没有说清楚,我一定会修改。

Please, let me know if more information is required, or this question isn't stated clearly enough and I will be sure to revise.

推荐答案

您应该使用提供商

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);

这篇关于Guice在不使用@Singleton的情况下将单个实例注入多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆