Guicing up Dropwizard

Implement dependency injection in Dropwizard with Google Guice..

Object Partners

Dropwizard is a lightweight RESTful web service framework built by Yammer. It incorporates a collection of mature Java libraries including Jetty, Jersey, Jackson, and Metrics to provide performant and reliable production-ready services.

At my current client, we’ve been implementing an entire micro-service backend using Dropwizard to drive content to Grails and AngularJS applications. One of the things that Dropwizard lacks is a succinct dependency injection implementation for your code. The latest released version of Dropwizard (0.6.2) uses Jersey v1.17.1 which had it’s own complicated DI framework (Jersey 2.x integrates HK2 which is a JSR-330 compliant framework).

We quickly found ourselves having to configure a substantial amount of code in our services just to wire classes together while allowing us to effectively and logically separate our business logic. So we’ve started exploring DI in Dropwizard. Guice is a lightweight DI framework. It also supports JSR-330 since v3.0.

We started integration Guice by utilizing the Dropwizard-Guice library from Hubspot. Simply add the dependency to your Dropwizard project and register the module:

class EchoService extends Service<EchoConfiguration> {

    public static void main(String[] args) {
        new EchoService().run(args)
    }

    @Override
    void initialize(Bootstrap<EchoConfiguration> bootstrap) {
        bootstrap.addBundle(
                GuiceBundle.<EchoConfiguration>newBuilder()
                        .addModule(new EchoModule())
                        .setConfigClass(EchoConfiguration)
                        .enableAutoConfig(this.class.package.name)
                        .build()
        )
    }

    @Override
    void run(EchoConfiguration configuration, Environment environment) throws Exception {

    }
}

This simple configuration does a couple things. First, it registers the Guice bundle with Dropwizard. This is turn will automatically register the following items with Jersey from your package:

  • HealthChecks (extends HealthCheck)
  • Tasks (extends Task)
  • Resources (has a @Path annotation)
  • Managed classes (extends Managed)
  • Injectable Providers (extends InjectableProvider)
  • Providers (extends Provider)
  • Bundles (extends Bundle)

During this process, Guice will inject any necessary dependencies into these classes (for those it has to instantiate at this point). Second, it registers a providers for both the Dropwizard Environment instance and the service’s Configuration instance. Finally, it registers a GuiceComponentProviderFactory with the Jersey IoC container.

One of the big wins with Guice is that it is capable of injecting any class that either has a no-arg constructor or has a constructor that is annotated with @Inject. This can help to quickly break up logic without having a proliferation of annotations to describe how your classes are wired up.

At this point, we had Guice wired into our services but we needed a way to inject the SessionFactory from the Dropwizard-Hibernate bundle into our DAOs. If you want to see how this was accomplished, check out my Dropwizard-Guice example project on Github.

Share this Post

Related Blog Posts

JVM

Parallel Grails Functional Tests with Geb and Gradle

November 14th, 2013

Speed up your Grails functional tests by running them in parallel with Gradle

Object Partners
JVM

Set up a pseudo test suite in Grails while doing a major upgrade

November 12th, 2013

Describes a technique for setting up a temporary test suite while upgrading a Grails application.

Object Partners
JVM

Testing GWT with GwtMockito

November 7th, 2013

How to write tests for GWT using GwtMockito. GwtMockito makes it possible for you to write Mockito unit tests for your GWT code; these tests execute a lot faster than GWTTestCase or PowerMockito tests, making your build times more scalable.

Neil Buesing

About the author