Does it make any sense to use the servlet scopes outside of a servlet?

71 views
Skip to first unread message

Hannes Steffenhagen

unread,
Jun 7, 2015, 3:12:08 PM6/7/15
to google...@googlegroups.com
Hello, I'm currently writing an Application using Guice. It is not a web application (nor is it based on servlets), but it still conceptually has scopes like 'Application', 'Session' and 'Request' - does it make any sense at all to try to attempt to use the built in scopes from the web module? Or should I rather write my own scopes, despite what the documentation recommends?

Jens

unread,
Jun 7, 2015, 5:32:36 PM6/7/15
to google...@googlegroups.com
The session scope stores data in the HTTP session and the request scope stores data as attribute on the HTTP servlet request. See: https://github.com/google/guice/blob/master/extensions/servlet/src/com/google/inject/servlet/ServletScopes.java

So if you don't have a HttpSession and HttpServletRequest in your application then you can not use these scopes.

-- J.

Hannes Steffenhagen

unread,
Jun 7, 2015, 9:14:59 PM6/7/15
to google...@googlegroups.com
I see. I guess the documentation saying that it's not recommended for users to write their own scopes made me thing the built in scopes were more general than they actually are.

Tim Boudreau

unread,
Jun 8, 2015, 2:29:14 AM6/8/15
to google...@googlegroups.com
On Sunday, June 7, 2015 at 3:12:08 PM UTC-4, Hannes Steffenhagen wrote:
Hello, I'm currently writing an Application using Guice. It is not a web application (nor is it based on servlets), but it still conceptually has scopes like 'Application', 'Session' and 'Request' - does it make any sense at all to try to attempt to use the built in scopes from the web module? Or should I rather write my own scopes, despite what the documentation recommends?

I've found it so useful to do that that I wrote a library for generically doing custom scopes:

To use it in a Maven project:

(one of these days I'll get that and a bunch of related stuff on Maven Central)

-Tim
 

Luke Sandberg

unread,
Jun 8, 2015, 10:17:01 AM6/8/15
to google...@googlegroups.com
I work on servers that have 'extended' the definition of @RequestScope beyond just servlet based http requests.  We use the same annotations but use alternate implementations of the Scope object.  So i think it would be quite reasonable to reimplement the scopes for your usecase.

The general advice around 'don't implement your own scopes' is accurate,  a Scope is generally a very general and all encompassing concept, so it is rare that you might have an application that needs something beyond the standard set.  Plus there can be weird issues introduced if your custom scopes have overlapping lifetimes.  So the advice is correct, it just may not apply to your usecase :)

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/8eac70b5-7dfa-4a34-9aa1-0aa9a97003c0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages