我一直试图利用Dropwizard & JDBI中的Guice & advantage,这样我就可以在我的DBI类中使用惰性的单例。然而,我一直在讨论各种各样的问题。我遵循了这里概述的创建GovernatorInjectorFactory:https://github.com/HubSpot/dropwizard-guice的说明
然而,我开始触及总督和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>Note I正在使用治理器版本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)但是,在我的应用程序中,我使用的设置与在下拉向导- 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 & 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
}
}备注包/导入已被排除用于投递,但所有这些类都存在于同一个包中。
发布于 2015-04-09 03:37:32
看来我错过了盖斯-多绑定模块管理器.
对于总督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提供特别的道具,帮助我找到这个修复程序:https://github.com/HubSpot/dropwizard-guice/issues/54
https://stackoverflow.com/questions/29514387
复制相似问题