简体   繁体   English

无法使用Guice和Vertx将相同的实例注入多个Verticle

[英]Cant Inject same instance to multiple Verticles using Guice and Vertx to

I have 3 vertx verticles. 我有3个vertx Verticle。

I created class AImpl which implements class A as follows 我创建了类AImpl,它实现了类A,如下所示

 @Singleton
public class AImpl implements A {


    public LocationServiceImpl() {
        System.out.println("initiated once");

    }

 public void doSomething(){..}

Verticle 1 looks like this: Verticle 1看起来像这样:

public class MyVerticle1 extends AbstractVerticle {
...
 @Inject
    private A a;


 @Override
    public void start(Future<Void> fut) {
 Guice.createInjector(new AppInjector()).injectMembers(this);
  a.doSomething(..);

..}

MyVerticle2 and MyVerticle3 looks the same. MyVerticle2和MyVerticle3看起来一样。

Guice code: Guice代码:

public class AppInjector extends AbstractModule {


    public AppInjector() {
    }


    @Override
    protected void configure() {   
 bind(A.class).to(AImpl.class).in(Singleton.class);

    }

Now when I run vertx I can see that I get 3 different instances of AImpl: 现在当我运行vertx时,我可以看到我得到3个不同的AImpl实例:

 public static void main(String[] args) throws InterruptedException {
        final Logger logger = Logger.getLogger(StarterVerticle.class);
        ClusterManager mgr = new HazelcastClusterManager();
        VertxOptions options = new VertxOptions().setClusterManager(mgr);
        Vertx.clusteredVertx(options, res -> {
            if (res.succeeded()) {
                Vertx vertx = res.result();
                vertx.deployVerticle(new MyVerticle1());
                vertx.deployVerticle(new MyVerticle2());
                vertx.deployVerticle(new MyVerticle3());
                logger.info("Vertx cluster started!");
            } else {
                logger.error("Error initiating Vertx cluster");
            }
        });

console: 安慰:

2015-09-15 16:36:15,611 [vert.x-eventloop-thread-0] INFO   - Vertx cluster started!
initiated once
initiated once
initiated once

What am I abusing with guice? 我用guice辱骂什么? why I dont get the same AImpl instance? 为什么我没有得到相同的AImpl实例?

Thanks, ray. 谢谢,雷。

You are using guice the wrong way. 你用错误的方式使用guice。 You are creating the MyVerticle instances via new and create the injector inside their start message. 您正在通过new创建MyVerticle实例,并在其开始消息中创建注入器。 Thus you end up with 3 injectors, each holding a singleton. 因此,你最终得到3个注射器,每个注射器都有一个单独的注射器。

You have to create the injector once in your main() method and then let guice handle the creation of the MyVerticles: 你必须在main()方法中创建一次注入器,然后让guice处理MyVerticles的创建:

Injector injector = Guice.createInjector(....);
...
vertx.deployVerticle(injector.getInstance(MyVerticle1.class);

now the injector creates only one instance for AImpl and reuses it for all @Inject AImpl locations. 现在,注入器只为AImpl创建一个实例,并将其重用于所有@Inject AImpl位置。 Remove the injectors from your start methods completely. 完全从启动方法中删除喷射器。

2 rules of thumb when working with guice: 使用guice时的2条经验法则:

  1. avoid using new 避免使用new
  2. try to use only one single injector located in your main() method 尝试仅使用main()方法中的一个注入器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM