用于第三方 servelet 的 Guice Singleton Servlet Binding 解决方法

Guice Singleton Servlet Binding work-around for third-party servelets

我正在尝试找出如何为我的代码单例绑定 servlet:

public class GuiceServletModule extends ServletModule {
    @Override
    protected void configureServlets() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("org.restlet.application", "com.mycomp.server.RestletApplication");
        serve("/rest/*").with(org.restlet.ext.servlet.ServerServlet.class, params);
        serve("/remote_api").with(com.google.apphosting.utils.remoteapi.RemoteApiServlet.class);
    }
}

这里的问题是应用程序需要服务的两个servelet 都是第三方库(Restlet 和GAE)。

抛出的异常是:

[INFO] javax.servlet.ServletException: Servlets must be bound as singletons. Key[type=org.restlet.ext.servlet.ServerServlet, annotation=[none]] was not bound in singleton scope.

servlet 是第三方库,至少目前无法修改,请问如何处理?是否有解决方法来完成这项工作?

解决方法是添加:

    bind(RemoteApiServlet.class).in(Scopes.SINGLETON);
    bind(ServerServlet.class).in(Scopes.SINGLETON);