我最终使用了 Google Guice,这是一个与 Jersey 很好地集成的轻量级 DI 框架。这是我必须做的:
首先,我在 pom.xml 中添加了依赖项:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.12</version>
<scope>compile</scope>
</dependency>
我想要一个 DAO 实现为带有接口的单例:
public interface MySingletonDao
{
// ... methods go here ...
}
和一个具体的实现:
@Singleton
public class ConcreteMySingletonDao implements MySingletonDao
{
// ... methods go here ...
}
像这样装饰资源类:
@Path("/some/path")
@RequestScoped
public class MyResource
{
private final MySingletonDao mySingletonDao;
@Inject
public MyResource(MySingletonDao mySingletonDao)
{
this.mySingletonDao = mySingletonDao;
}
@POST
@Produces("application/json")
public String post() throws Exception
{
// ... implementation goes here ...
}
}
创建了一个将执行绑定的类:
public class GuiceConfig extends GuiceServletContextListener
{
@Override
protected Injector getInjector()
{
return Guice.createInjector(new JerseyServletModule()
{
@Override
protected void configureServlets()
{
bind(MyResource.class);
bind(AnotherResource.class);
bind(MySingletonDao.class).to(ConcreteMySingletonDao.class);
serve("/*").with(GuiceContainer.class);
}
});
}
}
我使用 Jetty 而不是 Glassfish 来实际充当服务器。在我的功能测试中,这看起来像:
private void startServer() throws Exception
{
this.server = new Server(8080);
ServletContextHandler root =
new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
root.addEventListener(new GuiceConfig());
root.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
root.addServlet(EmptyServlet.class, "/*");
this.server.start();
}
来自EmptyServlet
Sunny Gleason 的示例代码作为答案给出:https ://stackoverflow.com/a/3296467——我最初有
root.addServlet(new ServletHolder(new ServletContainer(new PackagesResourceConfig("com.example.resource"))), "/*");
而不是线
root.addServlet(EmptyServlet.class, "/*");
但这导致 Jersey 尝试执行依赖注入而不是 Guice,这会导致运行时错误。