在球衣中注入构造函数

时间:2013-07-03 04:10:26

标签: java web jersey guice grizzly

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

GoogleModule类:

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

在启动灰熊服务器之前,我称之为声明。

 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)

1 个答案:

答案 0 :(得分:0)

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

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