public class GuiceSimpleScope extends Object implements com.google.inject.Scope
Important: Do not use outside of Soy code (treat as superpackage-private). Apply this scope with a try/finally block:
scope.enter();
try {
// explicitly seed some seed objects
scope.seed(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) or
seed(class, value) before the injector will be called upon to provide for this key.
For each key seeded with seed(), you must include a corresponding binding:
bind(key)
.toProvider(GuiceSimpleScope.<KeyClass>getUnscopedProvider())
.in(ScopeAnnotation.class);
| Modifier and Type | Class and Description |
|---|---|
static interface |
GuiceSimpleScope.WithScope |
| Constructor and Description |
|---|
GuiceSimpleScope() |
| Modifier and Type | Method and Description |
|---|---|
GuiceSimpleScope.WithScope |
enter()
Enters an occurrence of this scope.
|
void |
exit()
Exits the current occurrence of this scope.
|
<T> T |
getForTesting(com.google.inject.Key<T> key)
Gets a value in the current occurrence of this scope.
|
static <T> com.google.inject.Provider<T> |
getUnscopedProvider()
Returns a provider that always throws exception complaining that the object
in question must be seeded before it can be injected.
|
boolean |
isActive()
Whether we're currently in an occurrence of this scope.
|
<T> com.google.inject.Provider<T> |
scope(com.google.inject.Key<T> key,
com.google.inject.Provider<T> unscopedProvider) |
<T> void |
seed(Class<T> class0,
T value)
Seeds a value in the current occurrence of this scope.
|
<T> void |
seed(com.google.inject.Key<T> key,
T value)
Seeds a value in the current occurrence of this scope.
|
public static <T> com.google.inject.Provider<T> getUnscopedProvider()
@CheckReturnValue public GuiceSimpleScope.WithScope enter()
public void exit()
public boolean isActive()
public <T> void seed(com.google.inject.Key<T> key,
T value)
key - The key to seed.value - The value for the key.public <T> void seed(Class<T> class0, T value)
class0 - The class to seed.value - The value for the key.public <T> T getForTesting(com.google.inject.Key<T> key)
key - The key to get.public <T> com.google.inject.Provider<T> scope(com.google.inject.Key<T> key,
com.google.inject.Provider<T> unscopedProvider)
scope in interface com.google.inject.Scope