Dropwizard,JDBI,Guice,& Governator

时间:2015-04-08 12:17:14

标签: guice dropwizard

我一直在努力利用Guice& Dropwizard的统治者& JDBI以便我可以将懒惰的单例用于我的DBI类。但是,我一直在遇到各种各样的问题。我按照此处列出的说明创建了GovernatorInjectorFactory: https://github.com/HubSpot/dropwizard-guice

但是,我开始在Governator和Dropwizard之间遇到类路径问题。我必须在我的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>

注意我正在使用Governator版本1.3.3

但现在我遇到的问题是它没有指定任何基础软件包,乍一看并看到NoSuchMethodError我认为它可能是另一个类路径问题:

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)

但是,在我的应用程序中,我使用他们在dropwizard-guice github页面上显示的相同设置:

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

有没有人看到这个有什么问题?或者甚至更好,有没有人知道州长和工作的一个工作实例; Dropwizard 0.8.0我可以用来比较?我添加了我的ExampleModule&amp;以下是GovernatorInjectorFactory的完整性,以防我只是一个白痴并在那里做了一些蠢事:

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
    }
}

注意发布时已排除了包/导入,但所有这些类都存在于同一个包中。

1 个答案:

答案 0 :(得分:3)

看起来我错过了guice-multibindings模块,总督正在拉进来......

您需要为州长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>

Johnathon Haber的特别道具指向我这个修复: https://github.com/HubSpot/dropwizard-guice/issues/54