Skip to content

Adds support for Guice in Yammer's Dropwizard damn simple library for building production-ready RESTful web services.

License

Notifications You must be signed in to change notification settings

cirrus-dev/dropwizard-guice

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dropwizard-Guice

A simple DropWizard extension for integrating Guice via a bundle. It optionally uses classpath scanning courtesy of the Reflections project to discover resources and more to install into the dropwizard environment upon service start.

Usage

    <dependencies>
        <dependency>
            <groupId>com.hubspot.dropwizard</groupId>
            <artifactId>dropwizard-guice</artifactId>
            <version>0.7.0.2</version>
        </dependency>
    </dependencies>

Simply install a new instance of the bundle during your service initialization

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {

	public static void main(String[] args) throws Exception {
		new HelloWorldApplication().run(args);
	}

	@Override
	public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {

		GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
				.addModule(new HelloWorldModule())
				.setConfigClass(HelloWorldConfiguration.class)
				.build();

		bootstrap.addBundle(guiceBundle);
	}

    @Override
    public String getName() {
        return "hello-world";
    }

	@Override
	public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
        environment.jersey().register(HelloWorldResource.class);
        environment.healthChecks().register("Template", TemplateHealthCheck.class);
	}
}

Lastly, you can enable auto configuration via package scanning.

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {

	public static void main(String[] args) throws Exception {
		new HelloWorldApplication().run(args);
	}

	@Override
	public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {

		GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
				.addModule(new HelloWorldModule())
				.enableAutoConfig(getClass().getPackage().getName())
				.setConfigClass(HelloWorldConfiguration.class)
				.build();

		bootstrap.addBundle(guiceBundle);
	}

    @Override
    public String getName() {
        return "hello-world";
    }

	@Override
	public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
        // now you don't need to add resources, tasks, healthchecks or providers
        // you must have your health checks inherit from InjectableHealthCheck in order for them to be injected
	}
}

If you are having trouble accessing your Configuration or Environment inside a Guice Module, you could try using a provider.

public class HelloWorldModule extends AbstractModule {

    @Override
    protected void configure() {
        // anything you'd like to configure
    }

    @Provides
    public SomePool providesSomethingThatNeedsConfiguration(HelloWorldConfiguration configuration) {
        return new SomePool(configuration.getPoolName());
    }

    @Provides
    public SomeManager providesSomenthingThatNeedsEnvironment(Environment env) {
        return new SomeManager(env.getSomethingFromHere()));
    }
}

Please fork an example project if you'd like to get going right away.

Enjoy!

About

Adds support for Guice in Yammer's Dropwizard damn simple library for building production-ready RESTful web services.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%