Skip to content

Instantly share code, notes, and snippets.

@derekbassett
Created March 6, 2016 18:53
Show Gist options
  • Save derekbassett/712698cd6424e5ff04da to your computer and use it in GitHub Desktop.
Save derekbassett/712698cd6424e5ff04da to your computer and use it in GitHub Desktop.
An example of using Jersey's dependency injection framework in a DropWizard application.
package io.github.derekbassett.exampledropwizard
import com.mongodb.MongoClient;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
public class ExampleDropwizardApplication extends Application<ExampleDropwizardApplicationConfiguration> {
public static void main(final String[] args) throws Exception {
new ExampleDropwizardApplication().run(args);
}
@Override
public void initialize(final Bootstrap<ExampleDropwizardApplicationConfiguration> bootstrap) {
}
@Override
public void run(final ExampleDropwizardApplicationConfiguration configuration, final Environment environment) throws Exception {
final MongoClient mongoClient = configuration.getMongoClientFactory().buildClient(environment, "mongo");
environment.jersey().register(new AbstractBinder() {
@Override
protected void configure() {
bind(configuration).to(ExampleDropwizardApplicationConfiguration.class);
bind(mongoClient).to(mongoClient);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment