Class ChildContext
java.lang.Object
be.imgn.inject.context.ChildContext
- All Implemented Interfaces:
Context
A child
Context: nearest-ancestor local bindings first (walking up through any intermediate
ChildContexts), then the parent. Unscoped parent beans are rebuilt at this — the deepest — context so their
dependencies see the full child binding stack. A bean of a child-local scope (one with no Scope service) is
built once by the nearest context of the chain that hosts its scope, this one included, which caches it; its
dependencies resolve through that host. Other scoped beans and everything without a BeanDefinition anchor to
the parent unchanged.-
Constructor Summary
ConstructorsConstructorDescriptionChildContext(Context parent, Map<Key<?>, Provider<?>> bindings) Creates a child context overparent.ChildContext(Context parent, Map<Key<?>, Provider<?>> bindings, Class<? extends Annotation> scope) Creates a child context overparentthat hosts the child-localscope: it builds and caches the instances of that scope's beans resolved through it or its descendants. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this child context: rejects further resolution through it, including through the providers it returned, closes its live child contexts, newest first, then destroys the instances of its hosted scope (invoking each factory's@PreDestroy) in reverse creation order.<T> Tconstructed(T instance) Reports a fully built instance to theConstructionListeners, with the injector of this context.<T> TcreateScoped(ScopedProduct product, @Nullable Consumer<? super T> preDestroy, Function<? super Context, ? extends T> instance) Creates the instance of a child-local assisted product, and caches it in the nearest context of the chain that hosts its scope, by its key.getBeanDefinition(Key<?> key) Returns the definition bound tokey, if any.getBeanDefinitionsByStereotype(Class<? extends Annotation> stereotype) Returns every registered definition whose bean is annotated with an annotation carryingstereotype.<T> Provider<T> getProvider(Key<T> key) Returns the provider bound tokey.interceptor(Class<? extends Annotation> annotationType) Returns the provider for the interceptor handlingannotationType.voidsetInjector(Injector injector) Attaches theInjectorthis context resolvesKey.of(Injector.class)to.
-
Constructor Details
-
ChildContext
Creates a child context overparent.- Parameters:
parent- the parent context resolutions fall back tobindings- the child's own bindings, by key- Throws:
IllegalStateException- ifparentis closed
-
ChildContext
public ChildContext(Context parent, Map<Key<?>, Provider<?>> bindings, Class<? extends Annotation> scope) Creates a child context overparentthat hosts the child-localscope: it builds and caches the instances of that scope's beans resolved through it or its descendants.- Parameters:
parent- the parent context resolutions fall back tobindings- the child's own bindings, by keyscope- the child-local scope this context hosts- Throws:
IllegalArgumentException- ifscopeis handled by aScopeservice, or if the root of the chain is not aDefaultContextIllegalStateException- ifparentis closedScopeResolutionException- if a bean ofscopedepends directly on a bean of another child-local scope that no context of the chain hosts; the message lists every such dependencyUnsatisfiedDependencyException- if a bean ofscopedepends on a key that neither the root, nor this context, nor one of its parents binds; the message lists every such dependency
-
-
Method Details
-
setInjector
Attaches theInjectorthis context resolvesKey.of(Injector.class)to. Called once by the child injector that owns this context, immediately after construction.- Parameters:
injector- the owning injector- Throws:
IllegalStateException- if an injector is already attached
-
getProvider
Description copied from interface:ContextReturns the provider bound tokey.- Specified by:
getProviderin interfaceContext- Type Parameters:
T- the bound type- Parameters:
key- the binding key- Returns:
- the provider bound to
key
-
createScoped
public <T> T createScoped(ScopedProduct product, @Nullable Consumer<? super T> preDestroy, Function<? super Context, ? extends T> instance) Description copied from interface:ContextCreates the instance of a child-local assisted product, and caches it in the nearest context of the chain that hosts its scope, by its key. Generated assisted factories call this for a recipe with a child-local scope.The default rejects every call, so minimal
Contextimplementations need not host scopes.- Specified by:
createScopedin interfaceContext- Type Parameters:
T- the product type- Parameters:
product- the product to createpreDestroy- the product's@PreDestroycallback, run when its host closes, ornullinstance- builds the instance through the context it is given: the host's, so the product's dependencies resolve where it is cached- Returns:
- the created instance
-
getBeanDefinition
Description copied from interface:ContextReturns the definition bound tokey, if any.Introspection only; the default is empty so minimal
Contextimplementations need not supply it.- Specified by:
getBeanDefinitionin interfaceContext- Parameters:
key- the binding key, qualified or not- Returns:
- the definition bound to
key, if any
-
getBeanDefinitionsByStereotype
Description copied from interface:ContextReturns every registered definition whose bean is annotated with an annotation carryingstereotype.- Specified by:
getBeanDefinitionsByStereotypein interfaceContext- Parameters:
stereotype- the meta-annotation to filter by, such asQualifyingAnnotation.class- Returns:
- every registered definition whose bean is annotated with an annotation carrying
stereotype
-
interceptor
Description copied from interface:ContextReturns the provider for the interceptor handlingannotationType.The default rejects every lookup, so minimal
Contextimplementations need not supply an index.- Specified by:
interceptorin interfaceContext- Parameters:
annotationType- the intercepting annotation- Returns:
- the provider for the interceptor handling
annotationType
-
constructed
public <T> T constructed(T instance) Description copied from interface:ContextReports a fully built instance to theConstructionListeners, with the injector of this context. Generated factories call this for every instance they build. The default reports nothing.- Specified by:
constructedin interfaceContext- Type Parameters:
T- the instance type- Parameters:
instance- the fully built instance- Returns:
instance
-
close
public void close()Closes this child context: rejects further resolution through it, including through the providers it returned, closes its live child contexts, newest first, then destroys the instances of its hosted scope (invoking each factory's@PreDestroy) in reverse creation order. Failures are collected as suppressed exceptions on the first one thrown, so every child and every instance is still given a chance to close or destroy. Never closes the parent. Idempotent.
-