0

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

Nav
  • 4,450
  • 10
  • 53
  • 84

2 Answers2

1

Have you tried @SessionScoped?

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
1

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!

smjZPkYjps
  • 1,168
  • 8
  • 12
  • No, my question is how to configure non-singletone Servlet – Nav Sep 07 '12 at 12:47
  • I'm confused. You say "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." That's exactly what this answer will do. I don't understand what you mean by "how to configure non-singletone Servlet." Request scope is per request. There's no singletons involved. – smjZPkYjps Sep 07 '12 at 12:50
  • my question is how to configure non-singletone Servlet, I want to be created new servlet instance in case of new request when the first is still in process not inside objects used by servlet – Nav Sep 07 '12 at 13:58
  • It seems for each servlet class container just create one instance, I want to change it – Nav Sep 07 '12 at 13:58