我正在尋找一種方法來覆蓋Guice在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());
}
}
但不幸的是,這並不工作,但我不能在吉斯結合球衣資源,如接口的實現,只有bind(BookImpl.class)
工作的。但是這種綁定是不可能覆蓋的。如果我嘗試用bind(BookUserImpl.class)
覆蓋bind(BookImpl.class)
,則會收到錯誤Conflicting URI templates. The URI template /books/{key} for root resource class.
,而@Path應該是唯一的。那麼我的用例有沒有解決方案?