How do you create a non-Thread-based Guice custom Scope?
Posted
by Russ
on Stack Overflow
See other posts from Stack Overflow
or by Russ
Published on 2010-03-23T18:49:02Z
Indexed on
2010/03/23
18:53 UTC
Read the original article
Hit count: 437
It seems that all Guice's out-of-the-box Scope implementations are inherently Thread-based (or ignore Threads entirely):
Scopes.SINGLETON
and Scopes.NO_SCOPE
ignore Threads and are the edge cases: global scope and no scope.
ServletScopes.REQUEST
and ServletScopes.SESSION
ultimately depend on retrieving scoped objects from a ThreadLocal<Context>
. The retrieved Context
holds a reference to the HttpServletRequest
that holds a reference to the scoped objects stored as named attributes (where name is derived from com.google.inject.Key
).
Class SimpleScope
from the custom scope Guice wiki also provides a per-Thread implementation using a ThreadLocal<Map<Key<?>, Object>>
member variable.
With that preamble, my question is this: how does one go about creating a non-Thread-based Scope? It seems that something that I can use to look up a Map<Key<?>, Object>
is missing, as the only things passed in to Scope.scope() are a Key<T>
and a Provider<T>
.
Thanks in advance for your time.
© Stack Overflow or respective owner