Package org.spincast.core.guice
Class SpincastRequestScope
java.lang.Object
org.spincast.core.guice.SpincastRequestScope
- All Implemented Interfaces:
com.google.inject.Scope
Guice scope for a
request.
Modified from: https://github.com/google/guice/wiki/CustomScopes:
Scopes a single execution of a block of code. Apply this scope with a try/finally block:
scope.enter();
try {
// explicitly seed some seed objects...
scope.seed(Key.get(SomeObject.class), someObject);
// create and access scoped objects
} finally {
scope.exit();
}
The scope can be initialized with one or more seed values by calling
seed(key, value) before the injector will be called upon to
provide for this key. A typical use is for a servlet filter to enter/exit the
scope, representing a Request Scope, and seed HttpServletRequest and
HttpServletResponse. For each key inserted with seed(), you must include a
corresponding binding:
bind(key)
.toProvider(SimpleScope.<KeyClass>seededKeyProvider())
.in(ScopeAnnotation.class);
- Author:
- Jesse Wilson, Fedor Karpelevitch
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidenter()voidexit()getScopedObjectMap(com.google.inject.Key<T> key) static <T> com.google.inject.Provider<T>getSeedErrorProvider(com.google.inject.Key key) Returns a provider that always throws exception complaining that the object in question must be seeded before it can be injected.<T> com.google.inject.Provider<T>scope(com.google.inject.Key<T> key, com.google.inject.Provider<T> unscoped) <T> voidseed(com.google.inject.Key<T> key, T value) <T> voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.google.inject.Scope
toString
-
Field Details
-
logger
protected static final org.slf4j.Logger logger
-
-
Constructor Details
-
SpincastRequestScope
public SpincastRequestScope()
-
-
Method Details
-
enter
public void enter() -
exit
public void exit() -
seed
public <T> void seed(com.google.inject.Key<T> key, T value) -
seed
-
scope
public <T> com.google.inject.Provider<T> scope(com.google.inject.Key<T> key, com.google.inject.Provider<T> unscoped) - Specified by:
scopein interfacecom.google.inject.Scope
-
getScopedObjectMap
-
getSeedErrorProvider
public static <T> com.google.inject.Provider<T> getSeedErrorProvider(com.google.inject.Key key) Returns a provider that always throws exception complaining that the object in question must be seeded before it can be injected.- Returns:
- typed provider
-