0

我正在尝试使用 Guice 注入资源类的构造函数。同时,我使用 GrizzlyWebServer 作为服务器来处理来自资源类的请求。下面我给出了一些我遇到的代码和错误。

谷歌模块类:

public class GoogleModule extends AbstractModule {

  @Override
  protected void configure() {
    bind(Interface.class).to(Implementation.class);
  }
}

资源类

@Path("/path")
public class ResourceClass {
  Logger log = LoggerFactory.getLogger(ResourceClass.class);
  private final Interface interface;

  GConnection con = new GConnection();

  @Inject
  public ResourceClass(Interface interface) {
    this.interface = interface;
  }

  @GET
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  public List<Object> getObjects() {
    return  interface.list();
  }
}

在启动 grizzly 服务器之前,我调用了这个语句。

 Injector injector = Guice.createInjector(new GoogleModule());
 webserver.startServer();

设置完成后,我发现了以下错误。

  SEVERE: Missing dependency for constructor public        com.motilink.services.ResourceClass(Interface ) at parameter index 0
 Jul 3, 2013 9:52:36 AM com.sun.grizzly.http.servlet.ServletAdapter doService
SEVERE: service exception:
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at com.sun.grizzly.http.servlet.ServletAdapter.loadServlet(ServletAdapter.java:451)
at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:390)
at com.sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:347)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
at com.sun.grizzly.tcp.http11.GrizzlyAdapterChain.service(GrizzlyAdapterChain.java:186)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:824)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:721)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1014)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:220)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:530)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:511)
at java.lang.Thread.run(Thread.java:680)
4

1 回答 1

0

鉴于您正在通过 servlet 运行,您需要确保 Guice 已正确连接,以便通过注入而不是直接实例化来创建资源。您现在拥有代码的方式不用于创建资源的新实例,因此不会产生错误。

在Guice Wiki上有关于如何设置的详细信息。

于 2013-07-03T21:52:55.240 回答