Code Monkey home page Code Monkey logo

dropwizard-guice's Introduction

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()));
    }
}

You can also replace the default Guice Injector by implementing your own InjectorFactory. For example if you want to use Governator you can create the following implementation:

public class GovernatorInjectorFactory implements InjectorFactory
{
    @Override
    public Injector create( final Stage stage, final List<Module> modules )
    {
        return LifecycleInjector.builder().inStage( stage ).withModules( modules ).build()
            .createInjector();
    }
}

and then set the InjectorFactory when initializing the GuiceBundle:

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

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

		bootstrap.addBundle(guiceBundle);
	}

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

Enjoy!

dropwizard-guice's People

Contributors

andrew-m avatar axiak avatar bramp avatar dmorgantini avatar eliast avatar jhaber avatar mrserverless avatar

Watchers

 avatar  avatar

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.