Reputation: 334
I've been trying to take advantage of Guice & Governator with Dropwizard & JDBI so that I can use lazy singleton's for my DBI class. However, I've been hitting all sorts of issues. I've followed the instructions outlined here for creating the GovernatorInjectorFactory: https://github.com/HubSpot/dropwizard-guice
However, I start hitting classpath issues between Governator and Dropwizard. I've had to exclude the following modules in my pom.xml:
<dependency>
<groupId>com.netflix.governator</groupId>
<artifactId>governator</artifactId>
<version>${com.netflix.governator.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
Note I am using Governator version 1.3.3
But now I'm hitting issues where it's stating no base packages have been specified, which at first glance and seeing a NoSuchMethodError I'm thinking it may be yet another classpath issue:
WARN [2015-04-08 07:13:04,445]
com.netflix.governator.lifecycle.ClasspathScanner: No base packages specified - no classpath scanning will be done
Exception in thread "main" java.lang.NoSuchMethodError: com.google.inject.binder.AnnotatedBindingBuilder.toProvider(Ljavax/inject/Provider;)Lcom/google/inject/binder/ScopedBindingBuilder;
at com.squarespace.jersey2.guice.InternalJerseyModule.configure(InternalJerseyModule.java:58)
at com.google.inject.AbstractModule.configure(AbstractModule.java:59)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.AbstractModule.install(AbstractModule.java:118)
at com.squarespace.jersey2.guice.BootstrapModule.configure(BootstrapModule.java:44)
at com.google.inject.AbstractModule.configure(AbstractModule.java:59)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.AbstractModule.install(AbstractModule.java:118)
at com.hubspot.dropwizard.guice.JerseyModule.configureServlets(JerseyModule.java:15)
at com.google.inject.servlet.ServletModule.configure(ServletModule.java:55)
at com.google.inject.AbstractModule.configure(AbstractModule.java:59)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.spi.Elements.getElements(Elements.java:101)
at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:133)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:103)
at com.google.inject.internal.InjectorImpl.createChildInjector(InjectorImpl.java:217)
at com.netflix.governator.guice.LifecycleInjector.createChildInjector(LifecycleInjector.java:327)
at com.netflix.governator.guice.LifecycleInjector.createInjector(LifecycleInjector.java:394)
at com.netflix.governator.guice.LifecycleInjector.createInjector(LifecycleInjector.java:348)
at com.gordysc.GovernatorInjectorFactory.create(GovernatorInjectorFactory.java:15)
at com.hubspot.dropwizard.guice.GuiceBundle.initInjector(GuiceBundle.java:105)
at com.hubspot.dropwizard.guice.GuiceBundle.initialize(GuiceBundle.java:96)
at io.dropwizard.setup.Bootstrap.addBundle(Bootstrap.java:142)
at com.gordysc.ExampleApplication.initialize(ExampleApplication.java:22)
at io.dropwizard.Application.run(Application.java:71)
at com.gordysc.ExampleApplication.main(ExampleApplication.java:32)
However, inside my application, I'm using the same setup they show on the dropwizard-guice github page:
public final class ExampleApplication extends Application<ExampleConfiguration> {
private GuiceBundle<ExampleConfiguration> bundle;
@Override
public void initialize( io.dropwizard.setup.Bootstrap<ExampleConfiguration> bootstrap ) {
//@formatter:off
bundle = GuiceBundle.<ExampleConfiguration>newBuilder()
.addModule( new ExampleModule() )
.enableAutoConfig( getClass().getPackage().getName() )
.setConfigClass( ExampleConfiguration.class )
.setInjectorFactory( new GovernatorInjectorFactory() )
.build();
//@formatter:on
bootstrap.addBundle( bundle );
};
@Override
public void run( ExampleConfiguration configuration, Environment environment ) throws Exception {
// TODO Auto-generated method stub
}
public static void main( String[] args ) throws Exception {
new ExampleApplication().run( args );
}
}
Does anyone see anything wrong with this??? Or even better, does anyone know a working example for Governator & Dropwizard 0.8.0 I could use to compare against? I've added my ExampleModule & the GovernatorInjectorFactory below for completeness in case I'm just an idiot and did something dumb there:
final class ExampleModule extends AbstractModule {
@Provides
private DBIFactory dbiFactory() {
return new DBIFactory();
}
@Inject
@Provides
@LazySingleton
private DBI dbi( ExampleConfiguration configuration, Environment environment, DBIFactory factory ) {
return factory.build( environment, configuration.getDataSourceFactory(), "mysql" );
}
@Override
protected void configure() {
// TODO Auto-generated method stub
}
}
final class GovernatorInjectorFactory implements InjectorFactory {
@Override
public Injector create( final Stage stage, final List<Module> modules ) {
//@formatter:off
return LifecycleInjector.builder()
.inStage( stage )
.withModules( modules )
.build()
.createInjector();
//@formatter:on
}
}
Note Packages/imports have been excluded for posting, but all of these classes exist in the same package.
Upvotes: 0
Views: 1694
Reputation: 334
Looks like I missed the guice-multibindings module Governator was pulling in...
You will need to exclude the following for Governator 1.3.3:
<dependency>
<groupId>com.netflix.governator</groupId>
<artifactId>governator</artifactId>
<version>1.3.3</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
</exclusion>
</exclusions>
</dependency>
Special props to Johnathon Haber for pointing me to this fix: https://github.com/HubSpot/dropwizard-guice/issues/54
Upvotes: 3