java - 用 guice 覆盖 Jersey 资源

标签 java jersey guice guice-servlet

我正在寻找一种方法来覆盖在 GuiceServletContextListener 中与 guice 绑定(bind)的 Jersey 资源。我正在尝试运行的代码:

//Define Jersey resource interface
@Path("/books/{key}")
public interface BookDocument {

    public BookDAO getDao();

    public void setDao(BookDAO dao);
}

//Define default implementation
public class BookImpl implements Book {

    @Override
    public BookDAO getDao() {
        return dao;
    }

    @Inject
    @Override
    public void setDao(BookDAO dao) {
        this.dao = dao;
    }
}

//User wants to inject his implementation, so he define it
public class BookUserImpl implements Book {

    @Override
    public BookDAO getDao() {
        return dao;
    }

    @Inject
    @Override
    public void setDao(BookDAO dao) {
        this.dao = dao;
    }
}

//Inject default implementation of resource
public class ApplicationResourcesModule extends AbstractModule
{
    @Override
    protected void configure()
    {
        bind(Book).to(BookImpl);
    }
}

//But user wants to inject his implementation, so he bind it in users AbstractModule
public class ApplicationResourcesModuleUser extends AbstractModule
{
    @Override
    protected void configure()
    {
        bind(Book).to(BookUserImpl);
    }
}

//Bind all resources
public class JerseyGuiceConfig extends GuiceServletContextListener
{
    @Override
    protected Injector getInjector()
    {
        //Override default binding by user bindings.
        return Guice.createInjector(Modules.override(new ApplicationResourcesModule()).with(new ApplicationResourcesModuleUser()), new JerseyServletModule());
    }
}

但不幸的是,这行不通,虽然我不能像实现接口(interface)那样在 guice 中绑定(bind) Jersey 资源,只有 bind(BookImpl.class) 可以工作。但是这样的绑定(bind)是不可能被覆盖的。如果我尝试使用 bind(BookUserImpl.class) 覆盖 bind(BookImpl.class),我会收到错误消息 Conflicting URI templates。根资源类的 URI 模板/books/{key}。 而 @Path 应该是唯一的。那么我的用例有什么解决方案吗?

最佳答案

我只是想警告您 Modules.override 对 Guice.createInjector(Stage.PRODUCTION,...) 不起作用,因此您应该仅在开发时谨慎使用它。您应该创建两个上下文监听器并以某种方式(可以说是通过 maven 配置文件)设置 web.xml 并正确实现。

更好用:

//Inject default implementation of resource
public class MainModule extends AbstractModule
{
    @Override
    protected void configure()
    {
        if(currentStage().equals(Stage.PRODUCTION) {
          install(new ApplicationResourcesModuleUser());
        } else {
          install(new ApplicationResourcesModule());
        }
    }
}

//Bind all resources
public class JerseyGuiceConfigPROD extends GuiceServletContextListener
{
    @Override
    protected Injector getInjector()
    {
        //Override default binding by user bindings.
        return Guice.createInjector(Stage.PRODUCTION, new MainModule(), new JerseyServletModule());
    }
}

public class JerseyGuiceConfigDEV extends GuiceServletContextListener
{
    @Override
    protected Injector getInjector()
    {
        //Override default binding by user bindings.
        return Guice.createInjector(Stage.DEVELOPMENT, new MainModule(), new JerseyServletModule());
    }
}

您可以在您的界面上使用@ImplementedBy 注释来说明默认实现应该是什么。因此,您不必显式绑定(bind)它,如果您绑定(bind)它,它将覆盖注释绑定(bind)。

@Path("/books/{key}")
@ImplementedBy(BookImpl.class)
public interface Book {

    public BookDAO getDao();

    @Inject //it is enough to put the injection here, i think
    public void setDao(BookDAO dao);
}

我认为这个问题与 Book 和 Book 实现绑定(bind)无关,而是与将 servlet 绑定(bind)/注册到 Jersey 容器有关。你能粘贴整个堆栈跟踪吗,guice 堆栈跟踪很冗长但很有帮助。

关于java - 用 guice 覆盖 Jersey 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13752061/

相关文章:

java - 在 Controller 中设置 "systems"属性并在某个方面访问该属性

java - Maven 会从两个依赖项中选择最新的依赖项吗?

java - 如何使用 guice 注入(inject)来模拟 play 框架中的身份 validator ?

java - 如何混合使用 Guice 和 Jersey 注入(inject)?

Java构造函数模式

java - 如何从maven项目返回网页

java - Spring Batch 在处理每个批处理后是否释放堆内存?

java - 如何计算两个整数值的结果,但从 java 中的 jComboBox 获取加法或乘法运算符

java - WebLogic 12c 中的 Resteasy(或不是 Jersey - JAX RS 实现)可能吗?

java - org.hibernate.LazyInitializationException 关联集合