ef-labs / vertx-guice

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

Add the possibility to inject verticle's configuration #8

Closed hiddenbyte closed 8 years ago

hiddenbyte commented 8 years ago

Had a project in hands where I needed for the injected instances to have access to the verticle's config.

I think this feature might be usefull for more people and worth of adding to the vertx-guice. :)

Example:

vertx.deployVerticle(GuiceVerticleFactory.PREFIX + ":" +  "ExampleVerticle",
    new DeploymentOptions().setConfig(new JsonObject().put(GuiceVerticleFactory.GUICE_CONFIG, new JsonObject().put("api", new JsonObject().put("host", "localhost").put("port", 81)))),
    someHandler);

Injecting a configuration to an instance can be done using the Named annotation containing the "path" to the needed configuration, in the SomeClient class below config will contain a JsonObject with "{ host : "localhost", port: 81}". If you need the whole configuration JsonObject just use a Named annotation containing "config", in this case it will be "{ api: { host : "localhost", port: 81} }".

public class SomeClient  {
    @Inject
    public SomeClient(Vertx vertx, @Named("config.api") JsonObject config) {
        //Some initialisation code
    }
}

Injecting SomeClient into a Verticle

public class ExampleVerticle extends AbstractVerticle {
    @Inject
    public ExampleVerticle(SomeClient client) {
    }
}
reistiago commented 8 years ago

any update on this issue?

hiddenbyte commented 8 years ago

This no longer makes sense.

adrianluisgonzalez commented 8 years ago

I added an example showing how this can be achieved from the outside.