public class Opt4JScope
extends java.lang.Object
implements com.google.inject.Scope
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);
Constructor and Description |
---|
Opt4JScope() |
Modifier and Type | Method and Description |
---|---|
void |
enter() |
void |
exit() |
<T> com.google.inject.Provider<T> |
scope(com.google.inject.Key<T> key,
com.google.inject.Provider<T> unscoped) |
<T> void |
seed(java.lang.Class<T> clazz,
T value) |
<T> void |
seed(com.google.inject.Key<T> key,
T value) |
static <T> com.google.inject.Provider<T> |
seededKeyProvider()
Returns a provider that always throws exception complaining that the object
in question must be seeded before it can be injected.
|
public void enter()
public void exit()
public <T> void seed(com.google.inject.Key<T> key, T value)
public <T> void seed(java.lang.Class<T> clazz, T value)
public <T> com.google.inject.Provider<T> scope(com.google.inject.Key<T> key, com.google.inject.Provider<T> unscoped)
scope
in interface com.google.inject.Scope
public static <T> com.google.inject.Provider<T> seededKeyProvider()