quarks
quarks

Reputation: 35276

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

I'm trying to figure out how to Singleton bind a servlet for my code:

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);
    }
}

The problem here is that both servelets the app needs to serve is a third-party library (Restlet and GAE).

The exception thrown is:

[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.

How can I deal with this when the servlets is a third-party library that can't be modified at least at this point. Is there a work-around to make this work?

Upvotes: 9

Views: 1551

Answers (2)

Meh
Meh

Reputation: 141

This can also be resolved by adding @Singleton above the class definition like this:

@Singleton
public class GuiceServletModule extends ServletModule {

Upvotes: 0

quarks
quarks

Reputation: 35276

The solution is to add:

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

Upvotes: 13

Related Questions