我正在寻找一种方法来覆盖与 GuiceServletContextListener 中的 guice 绑定的球衣资源。我正在尝试使用的代码:
//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());
}
}
但不幸的是,这不起作用,虽然我不能将球衣资源绑定到 guice 中,比如接口到实现,只能bind(BookImpl.class)
工作。但是这样的绑定是不可能被覆盖的。如果我尝试覆盖bind(BookImpl.class)
我bind(BookUserImpl.class)
会得到一个错误Conflicting URI templates. The URI template /books/{key} for root resource class.
,而 @Path 应该是唯一的。那么我的用例有什么解决方案吗?