ef-labs / vertx-guice

Create Vert.x Modules and Verticles with dependency injection using Guice
MIT License
59 stars 24 forks source link

Singleton scope instance/bindinds (com.google.inject.CreationException) #10

Closed hiddenbyte closed 8 years ago

hiddenbyte commented 8 years ago

Using vertx-guice 2.3.0, we currently can not have singleton scope binding of types that depends on io.vertx.core.Vertx. By "singleton scope", I mean sharing a singleton instance along many Verticle.

One way to achieve this is by getting a GuiceVerticleFactory from this.getVertx().verticleFactories() and getting the setting the current injector (At "application start"), with the following bindings:

bind(io.vertx.core.Vertx.class).toInstance(vertx);
bind(SomeSigletonDependsOnVertex.class).in(Scopes.SINGLETON);

SomeSigletonDependsOnVertx will be singleton across all verticles (created by GuiceVerticleFactory).

This will throw an Exception com.google.inject.CreationException at com.englishtown.vertx.guice.GuiceVertxBinder because this binding is already configured in parent Injector.

One way to fix this is move com.englishtown.vertx.guice.GuiceVertxBinder binding to the parent injector.

Open for other solutions...

hiddenbyte commented 8 years ago

ping

adrianluisgonzalez commented 8 years ago

I believe I originally had the GuiceVertxBinder in the GuiceVerticleFactory and always created a parent injector, but this caused a problem...but I don't recall what it was.

I have pushed up a commit 9b7b7cb0b00ff5c0049923e6c4feb2ac62221acf with integration test that should address your issue. Does that work for you?

jklingsporn commented 8 years ago

Works for me.

adrianluisgonzalez commented 8 years ago

Released 2.3.1

hiddenbyte commented 8 years ago

Many thanks @adrianluisgonzalez ! :)