Interface Context
- All Superinterfaces:
AutoCloseable, Binder, Injector
An
Injector that manages zero or more Bindings to be used for injection into InjectionPoints.
Contexts are the preferred means by which applications perform
Dependency Injection. Once established,
applications typically use the following steps with a Context to perform injection:
- Invoke the
Binder.bind(Class)method to create and register theBindings, representing theObjects to be injected for specificClasses - Invoke the
Injector.inject(Object)method to perform injection into existingObjects - Invoke the
create(Class)method to instantiate an instance ofClass, using constructor-based injection, and afterwards using field or setter-based injection
- Since:
- Oct-2024
- Author:
- brian.oliver
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddResolver(Resolver<?> resolver) Adds the specifiedResolveraddResolver(BiFunction<? super InjectionFramework, ? super Context, Resolver<?>> supplier) <T> BindingBuilder<T> bind(T value) Creates aBindingBuilderpre-loaded with the specified instance value, enablingBindingBuilder.asAllInterfaces()andBindingBuilder.asAllInterfaces(Predicate)to register the value against every interface in its type hierarchy in a single call.voidclose()Closes thisContext, invoking anyPreDestroylifecycle methods on instantiated singleton instances in reverse dependency order (dependents are destroyed before their dependencies).<T> Tcreate(Dependency dependency) Creates an instance of the type represented by the specifiedDependencywith values resolved from theContext.<T> Tcreate(build.codemodel.foundation.usage.TypeUsage typeUsage) Creates an instance of the type represented by aTypeUsagewith values resolved from theContext.<T> TCreates an instance of the specified class by locating and injecting a constructor annotated withInjectwith values resolved from theContext.Pre-creates allSingleton-scoped class bindings in dependency order, usingGraphs.parallelizableGroups(VertexGraph)to initialize independent groups in parallel.resolver()Builds aBindingGraphTraitfrom the current contributor state, attaches it to the framework'sJDKCodeModel, and writes a human-readable wiring report tooutputPath.validate()Validates the current set of registered bindings before any objects are created.
-
Method Details
-
bind
Creates aBindingBuilderpre-loaded with the specified instance value, enablingBindingBuilder.asAllInterfaces()andBindingBuilder.asAllInterfaces(Predicate)to register the value against every interface in its type hierarchy in a single call.Example:
context.bind(myService).asAllInterfaces();- Type Parameters:
T- the type of the instance- Parameters:
value- the instance to bind- Returns:
- a
BindingBuilderloaded with the value
-
create
Creates an instance of the specified class by locating and injecting a constructor annotated withInjectwith values resolved from theContext. Should an injectable constructor not be found, an attempt will be made to use thedefaultconstructor.Should constructor injection succeed, the further injection of
Inject-annotated fields and methods will be attempted.- Type Parameters:
T- the type of theClass- Parameters:
requiredClass- the requiredClassto create- Returns:
- a new instance of the required
Class - Throws:
NullPointerException- when the required class wasnullInjectionException- when injection fails
-
create
Creates an instance of the type represented by aTypeUsagewith values resolved from theContext.- Type Parameters:
T- the type to create- Parameters:
typeUsage- theTypeUsage- Returns:
- an instance of the type represented by the
TypeUsage - Throws:
NullPointerException- when theTypeUsagewasnullInjectionException- when injection fails
-
create
Creates an instance of the type represented by the specifiedDependencywith values resolved from theContext.- Type Parameters:
T- the type to create- Parameters:
dependency- theDependency- Returns:
- an instance of the type represented by the
Dependency - Throws:
NullPointerException- when theDependencywasnullInjectionException- when injection fails
-
snapshot
Builds aBindingGraphTraitfrom the current contributor state, attaches it to the framework'sJDKCodeModel, and writes a human-readable wiring report tooutputPath.No-ops immediately if no real
BindingGraphContributoris installed (i.e. theBindingGraphContributor.NOOPis still in place). May be called at any point after bindings are registered.- Parameters:
outputPath- the path to write the wiring report to- Returns:
- this
Contextfor fluent chaining - Throws:
UncheckedIOException- if writing the report fails
-
validate
Context validate()Validates the current set of registered bindings before any objects are created. Performs three checks:- Cycle detection — throws
CyclicDependencyExceptionwith the full cycle path if a dependency cycle is found among class bindings. - Unsatisfied dependency detection — collects every class binding whose injected
dependencies have no corresponding binding and throws
ValidationExceptionlisting them all at once. - Scope violation detection — flags edges where a wider-scoped binding (e.g.
Singleton) depends on a narrower-scoped one (e.g. prototype).
- Returns:
- this
Contextfor fluent chaining (e.g.context.validate().initializeEagerSingletons()) - Throws:
CyclicDependencyException- if a dependency cycle is detectedValidationException- if unsatisfied dependencies or scope violations are found
- Cycle detection — throws
-
initializeEagerSingletons
Context initializeEagerSingletons()Pre-creates allSingleton-scoped class bindings in dependency order, usingGraphs.parallelizableGroups(VertexGraph)to initialize independent groups in parallel.Typically called immediately after
validate():context.validate().initializeEagerSingletons();- Returns:
- this
Contextfor fluent chaining
-
close
void close()Closes thisContext, invoking anyPreDestroylifecycle methods on instantiated singleton instances in reverse dependency order (dependents are destroyed before their dependencies).- Specified by:
closein interfaceAutoCloseable
-
newContext
-
resolver
-
addResolver
-
addResolver
- Parameters:
supplier- theBiFunctionto supply aResolverbased on theInjectionFrameworkandContext- Returns:
- this
Contextto permit fluent-style method invocation
-