Class InjectionContext

  • All Implemented Interfaces:
    Serializable
    Direct Known Subclasses:
    TrackableContext

    public abstract class InjectionContext
    extends Object
    implements Serializable
    Stores objects scoped to the context of some processing/call/request/session, such as an RPC, a servlet request processing, a session combining several calls etc. Stored objects can be obtained via Guice injections configured with bindings scoped in the associated ContextScope.

    Note: most context classes should rather extend TrackableContext subclass instead of this one. The main exception are context types that are induced by other contexts: see InducedContextScope.

    Subclasses usually add properties and methods specific to their type, like their call's arguments, a reference to their session object etc.

    Multiple threads may run within the same context, but the scoped objects that they access must be thread-safe or properly synchronized.

    See Also:
    Serialized Form
    • Constructor Detail

      • InjectionContext

        public InjectionContext()
    • Method Detail

      • removeScopedObject

        public void removeScopedObject​(Key<?> key)
        Removes the object given by key from this context. This is sometimes useful to force the associated ContextScope to obtain a new instance from the unscoped provider if the current one is not usable anymore (for example a timed-out connection, etc).

        Note: If multiple threads run within the same context, care must be taken to prevent some of them from retaining the old stale instances.

      • provideIfAbsent

        protected <T> T provideIfAbsent​(Key<T> key,
                                        Provider<T> provider)
        Obtains the object given by key. If there is a stored instance in this context, it is returned immediately. Otherwise, a new instance is obtained from provider and stored for subsequent calls.