Guice Singleton Servlet绑定适用于第三方服务

xyb*_*rek 8 java google-app-engine servlets guice

我试图弄清楚如何使用Singleton为我的代码绑定一个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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是应用程序需要提供的两个servlet都是第三方库(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.
Run Code Online (Sandbox Code Playgroud)

当servlet是至少在此时无法修改的第三方库时,我该如何处理这个问题.是否有解决方法使这项工作?

xyb*_*rek 12

解决方案是添加:

    bind(RemoteApiServlet.class).in(Scopes.SINGLETON);
    bind(ServerServlet.class).in(Scopes.SINGLETON);
Run Code Online (Sandbox Code Playgroud)