Guice and non-Singleton Servlets
-
30-06-2021 - |
Question
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
Solution
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!
OTHER TIPS
Have you tried @SessionScoped?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow