Nav
Nav

Reputation: 4540

Guice and non-Singleton Servlets

How we can use non-Singleton servlet or handler in Guice, for example I want to create new instance for every request and immediately destroy it or give it to garbage collector after processing the request.

I dont want to keep ii in memory as singleton or reuse for other future requests.

I think probably somehow by using bind() function,

RGDS

Upvotes: 0

Views: 361

Answers (2)

smjZPkYjps
smjZPkYjps

Reputation: 1178

You want to use RequestScope.

I typically use provider methods with scopes, so you would have code that looks like

public class FooModule extends AbstractModule {
  @Override protected void configure() {
    // set up bindings
  }

  @Provides
  @RequestScoped
  PerRequestObject providePerRequestObject() {
    return new PerRequestObject(...);
  }
}

Be sure to install ServletModule and setup the GuiceFilter or else this won't work!

Upvotes: 1

Jan Galinski
Jan Galinski

Reputation: 12013

Have you tried @SessionScoped?

Upvotes: 1

Related Questions