Code Monkey home page Code Monkey logo

mod-guice's Introduction

mod-guice README

mod-guice is a Google Guice module for Vert.x. It includes everything you need to be Guicing your Vert.x like a pro in five minutes.

The easy tutorial

First, create a Guice Module that implements VertxModule

public class MyModule implements VertxModule {

    private Container container;
    private Vertx vertx;

    @Override
    public void configure(Binder binder) {
		///Do binding stuff here!
		binder.bind(MyService.class).to(MyServiceImpl.class);
    }

    @Override
    public void setContainer(Container container) {
        this.container = container;
    }

    @Override
    public void setVertx(Vertx vertx) {
        this.vertx = vertx;
    }
}

Next create your Verticle, extending GuiceVerticle. Using the @GuiceVertxBinding annotation, supply a list of all the Modules the verticle needs at runtime. Use the @Inject annotation to inject any dependencies defined in the modules. You should @Override the onStart() method to kick off any of your normal verticle activity.

@GuiceVertxBinding(modules = {MyModule.class})
public class MyVerticle extends GuiceVerticle {

    @Inject
    MyService myService;

    @Override
    public void onStart() {
       	myService.doStuff();
    }
}

Make sure that you include the name of the module in your mod.xml for any module that needs to use mod-guice.

com.alienos.vertx~mod-guice~1.0.0-beta1

But wait, my Verticles already inherit from some other class!

Not a problem, here is an example for that case.

@GuiceVertxBinding(modules = {MyModule.class})
public class RawVerticle extends Verticle {

    @Inject
    MyService myService;

    @Override
    public void start() {
    	//Just call this firt from the start method
        GuiceVerticleHelper.inject(this, vertx, container);

		//Proceed as normal
		myService.doStuff();
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.