Dropwizard、JDBI、Guice 和 Governator

Dropwizard, JDBI, Guice, & Governator

我一直在尝试将 Guice & Governator 与 Dropwizard & JDBI 结合使用,以便我可以将惰性单例用于我的 DBI class。但是,我遇到了各种各样的问题。我已按照此处概述的说明创建 GovernatorInjectorFactory: https://github.com/HubSpot/dropwizard-guice

但是,我开始遇到 classGovernator 和 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 我认为这可能是另一个 class 路径问题:

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

有人看出这有什么问题吗???或者更好的是,有人知道我可以用来比较的 Governator & Dropwizard 0.8.0 的工作示例吗?为了完整起见,我在下面添加了我的 ExampleModule 和 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
    }
}

注意 Packages/imports 已被排除在外,但所有这些 class 都存在于同一个包中。

看来我错过了 Governator 引入的 guice-multibindings 模块...

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

特别感谢 Johnathon Haber 为我指出此修复程序: https://github.com/HubSpot/dropwizard-guice/issues/54