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(BookImpl.class)
、@ Pathが一意であるはずなbind(BookUserImpl.class)
のに、エラーが発生します。Conflicting URI templates. The URI template /books/{key} for root resource class.
それで、私のユースケースの解決策はありますか?