简体   繁体   English

Dropwizard,JDBI,Guice和Governor

[英]Dropwizard, JDBI, Guice, & Governator

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. 我一直在尝试与Dropwizard和JDBI一起使用Guice&Governator,以便我可以在我的DBI类中使用惰性单例。 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 我已经按照此处概述的说明创建了GovernatorInjectorFactory: https : //github.com/HubSpot/dropwizard-guice

However, I start hitting classpath issues between Governator and Dropwizard. 但是,我开始遇到Governor和Dropwizard之间的类路径问题。 I've had to exclude the following modules in my pom.xml: 我必须在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 注意我正在使用Governor 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: 但是现在我遇到了一个问题,即未指定基本包,乍一看并看到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)

However, inside my application, I'm using the same setup they show on the dropwizard-guice github page: 但是,在我的应用程序内部,我使用的是与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 );
    }
}

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? 甚至更好的是,有人知道我可以用来比较的Governor&Dropwizard 0.8.0的工作示例吗? I've added my ExampleModule & the GovernatorInjectorFactory below for completeness in case I'm just an idiot and did something dumb there: 为了完整性起见,我在下面添加了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
    }
}

Note Packages/imports have been excluded for posting, but all of these classes exist in the same package. 注意包/导入已被排除在发布之外,但是所有这些类都存在于同一包中。

Looks like I missed the guice-multibindings module Governator was pulling in... 似乎我错过了Governor引入的guice-multibindings模块...

You will need to exclude the following for Governator 1.3.3: 您将需要针对Governor 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 Johnathon Haber向我指出此修复程序的特殊道具: https : //github.com/HubSpot/dropwizard-guice/issues/54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM