Interface Injector
- All Superinterfaces:
AutoCloseable
Each create() (or builder() Injector.Builder.build()) call produces a fresh, independent graph:
its singletons are created once within that injector and are not shared with any other injector. Discard an
injector to discard its singletons.
Prefer constructor injection: annotate a single constructor with @Inject so a bean's dependencies are
final, explicit, and available the moment it is constructed. Field and method injection exist for cases constructor
injection cannot reach (e.g. framework-managed or cyclic wiring) and leave a window where the bean is only partly
initialised.
The injector itself is injectable: any bean may declare an Injector (or Provider<Injector>)
dependency and receives the injector that built it. Injector, Context, and Factory can never
be bound.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic Injector.Builderbuilder()Returns a builder for an injector whose instance bindings shadow discovered ones (a test seam).Returns a builder for a child of this injector: its bindings layer over this injector's graph.voidclose()Closes this injector: rejects further resolution through it, including through the providers it returned, closes its live child injectors, newest first, then invokes@PreDestroyon every scoped instance it created, in reverse creation order.static Injectorcreate()Returns a new injector backed by all registered factories and scopes.getBeanDefinition(Key<?> key) Returns the definition bound tokey, if any.getBeanDefinitionsByStereotype(Class<? extends Annotation> stereotype) Returns every bean annotated with an annotation carryingstereotype.<T> TgetInstance(Key<T> key) Returns an instance forkey.<T> TgetInstance(TypeLiteral<T> type) Returns an instance of the generictype.<T> TgetInstance(Class<T> type) Returns an instance oftype.<T> Provider<T> getProvider(Key<T> key) Returns the provider forkey.<T> Provider<T> getProvider(TypeLiteral<T> type) Returns a provider of the generictype.<T> Provider<T> getProvider(Class<T> type) Returns a provider oftype.scopedChildBuilder(Class<? extends Annotation> scope) Returns a builder for a child of this injector that hosts the child-localscope: its bindings layer over this injector's graph, and it builds and caches the instances ofscope's beans resolved through it or its descendants.
-
Method Details
-
getInstance
Returns an instance oftype.- Type Parameters:
T- the requested type- Parameters:
type- the requested type- Returns:
- an instance of
type
-
getInstance
Returns an instance forkey.- Type Parameters:
T- the requested type- Parameters:
key- the requested key- Returns:
- an instance for
key
-
getInstance
Returns an instance of the generictype.- Type Parameters:
T- the requested type- Parameters:
type- the requested type literal- Returns:
- an instance of the generic
type
-
getProvider
-
getProvider
Returns a provider of the generictype.- Type Parameters:
T- the requested type- Parameters:
type- the requested type literal- Returns:
- a provider of the generic
type
-
getProvider
-
getBeanDefinition
Returns the definition bound tokey, if any.A read-only view for inspecting the graph — the bean's key, declared framework annotations, and injection points — not for building instances.
- Parameters:
key- the binding key, qualified or not- Returns:
- the definition bound to
key, if any
-
getBeanDefinitionsByStereotype
Returns every bean annotated with an annotation carryingstereotype.For example
getBeanDefinitionsByStereotype(ScopingAnnotation.class)lists every scoped bean.- Parameters:
stereotype- the meta-annotation to filter by- Returns:
- every bean annotated with an annotation carrying
stereotype
-
childBuilder
Injector.ChildBuilder childBuilder()Returns a builder for a child of this injector: its bindings layer over this injector's graph.- Returns:
- a builder for a child of this injector: its bindings layer over this injector's graph
-
scopedChildBuilder
Returns a builder for a child of this injector that hosts the child-localscope: its bindings layer over this injector's graph, and it builds and caches the instances ofscope's beans resolved through it or its descendants.A scoping annotation is child-local when no
Scopeservice handles it. Each bean of such a scope is created once per hosting child, built through that child, and cached there. Resolving it through an injector with no hosting child in its chain fails with aScopeResolutionException. Hosting children nest: the nearest one in the chain hosts the bean.- Parameters:
scope- the child-local scope the child hosts- Returns:
- a builder for a child of this injector that hosts
scope
-
close
void close()Closes this injector: rejects further resolution through it, including through the providers it returned, closes its live child injectors, newest first, then invokes@PreDestroyon every scoped instance it created, in reverse creation order. A child injector's scoped instances are those of the scope it hosts. When a callback or a child fails, the others still run, and the first failure is thrown with the later ones suppressed. Closing a child never closes its parent. Idempotent.- Specified by:
closein interfaceAutoCloseable
-
create
Returns a new injector backed by all registered factories and scopes.- Returns:
- a new injector backed by all registered factories and scopes
- Throws:
UnsatisfiedDependencyException- if a bean depends on a key nothing binds; the message lists every such dependency. The dependencies of a bean of a child-local scope are checked when a child hosting that scope is built instead.RuntimeException- any exception thrown building a construction listener or anEagersingleton, once the singletons already built are destroyed
-
builder
Returns a builder for an injector whose instance bindings shadow discovered ones (a test seam).- Returns:
- a builder for an injector whose instance bindings shadow discovered ones (a test seam)
-