Dropwizard - 无法使用Guice Injector.getIntance()

时间:2016-04-01 13:10:29

标签: java rest jersey guice dropwizard

我已经在以前的方法中成功地使用了这个设置,因此我现在可能做错了。我主要使用Guice作为其AOP部分。

pom.xml的相关部分:

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>0.7.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.logging</groupId>
                <artifactId>jboss-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.0</version>
    </dependency>

    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
        <scope>provided</scope>
    </dependency>

BaseBindingModule:

public class BaseBindingModule extends AbstractModule{

    @Override
    protected void configure() {
        bindInterceptor(any(), annotatedWith(SendCrashReport.class), new CrashReport());    
        bind(CabManager.class).to(CabManagerImpl.class);
    }
}

资源注册的文件:

public class BucService extends Application<BucConfiguration> {
    public static void main(String[] args) throws Exception {
        new BucService().run(args);
    }

    @Override
    public void initialize(Bootstrap<BucConfiguration> bootstrap) {
        //bootstrap.setName("hello-world");
    }

    @Override
    public void run(BucConfiguration configuration,
                    Environment environment) {
        final String template = configuration.getTemplate();
        final String defaultName = configuration.getDefaultName();
        environment.jersey().register(new HelloWorldResource(template, defaultName));

        Injector injector = Guice.createInjector(new BaseBindingModule ());
        environment.jersey().register(injector.getInstance(CabResource.class)); // this does not work
      //  environment.jersey().register(new CabResource()); // this works
    }
}

实际资源文件:

@Path("/cabs")
@Produces(MediaType.APPLICATION_JSON)
public class CabResource {

    @Inject
    CabManager manager;


    public CabResource() {
    }

    @GET
    @Path("price")
    @Produces(MediaType.APPLICATION_JSON)
    public List<PriceEstimate> getCheapestCabs() {

        return manager.callB();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @SendCrashReport
    public List<PriceEstimate> getNearestCabs(@Context UriInfo uriInfo) {

        return manager.callA();
      }

}

当我尝试使用Guice Injector.getInstance()注册资源时,它没有注册,我无法访问任何REST API。

但是,如果我只是将资源注册为新的CabResource(),那么API就是可见的。但在这种情况下,我不能按照这个答案使GUICE AOP工作: https://stackoverflow.com/a/8862599/1443801

我错过了什么?

感谢。

编辑:这是我运行jar文件时显示的内容。请注意,没有cab API。

INFO  [2016-04-01 13:26:14,785] io.dropwizard.server.ServerFactory: Starting BucService
INFO  [2016-04-01 13:26:14,876] org.eclipse.jetty.setuid.SetUIDListener: Opened application@758a34ce{HTTP/1.1}{0.0.0.0:9000}
INFO  [2016-04-01 13:26:14,876] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@7ec3394b{HTTP/1.1}{0.0.0.0:9001}
INFO  [2016-04-01 13:26:14,877] org.eclipse.jetty.server.Server: jetty-9.0.z-SNAPSHOT
INFO  [2016-04-01 13:26:14,965] com.sun.jersey.server.impl.application.WebApplicationImpl: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM'
INFO  [2016-04-01 13:26:15,060] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    GET     /hello-world (com.buc.resources.HelloWorldResource)

INFO  [2016-04-01 13:26:15,262] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@6c451c9c{/,null,AVAILABLE}
INFO  [2016-04-01 13:26:15,263] io.dropwizard.setup.AdminEnvironment: tasks = 

    POST    /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)

WARN  [2016-04-01 13:26:15,264] io.dropwizard.setup.AdminEnvironment: 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!    THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW      !
!     IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE      !
!    LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR    !
!         APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT.       !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO  [2016-04-01 13:26:15,269] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@3dddbe65{/,null,AVAILABLE}
INFO  [2016-04-01 13:26:15,279] org.eclipse.jetty.server.ServerConnector: Started application@758a34ce{HTTP/1.1}{0.0.0.0:9000}
INFO  [2016-04-01 13:26:15,280] org.eclipse.jetty.server.ServerConnector: Started admin@7ec3394b{HTTP/1.1}{0.0.0.0:9001}

根据评论进一步澄清:

我确实使用injector.getInstance(CabResource.class)获取了一个实例。

例如:

CabResource a = new CabResource();
CabResource b = injector.getInstance(CabResource.class);

a.getCheapestCabs(); 
b.getCheapestCabs();

这两个电话都有效。

1 个答案:

答案 0 :(得分:0)

您是否考虑过使用https://github.com/HubSpot/dropwizard-guice

<dependency>
  <groupId>com.hubspot.dropwizard</groupId>
  <artifactId>dropwizard-guice</artifactId>
  <version>${dropwizard.guice.version}</version>
</dependency>
像这家伙那样 https://gitlab.com/michael-lloyd-lee/dropwizard-guice-example/tree/master