public final class DiContainer extends Object implements com.hazelcast.nio.Disposable
A container which simplifies the process of "wiring" a set of collaborator objects (providing each object with references to all its collaborators), as well as basic lifecycle management (initialization and disposal).
Objects are registered with the container by calling one of the dep(...) methods. An object can be
registered either by type or by name. If no explicit name or type is provided, then the object's actual
type is used. Likewise, an object specifies its dependency by declaring its registered type or name.
Type hierarchy is not considered when resolving by type; a dependency must be referred to by its registered type.
The container can accept either an already constructed object or a class that it will instantiate
itself. It will instantiate a class by calling a constructor annotated with Inject. A class
There are two main types of wiring:
final fields, but is vulnerable to dependency cycles.
@Inject-annotated fields and/or by passing them to @Inject-annotated methods. This forces
the fields to be mutable, but is unaffected by dependency cycles.
final fields.
An object may need some initialization that uses late-injected dependencies. This is the purpose of the
Initialize method annotation. wireAndInitializeAll() will first late-wire all registered objects,
then call all @Initialize methods on each object. An @Initialize method can specify parameters,
these will be provided by the container with the same semantics as for @Inject methods.
Objects will be wired and initialized in the same order in which they were registered.
An object may also be submitted for automatic disposal by calling disposable() immediately after
the dep(...) call that registers it. The object must implement Disposable. When this container's
dispose() method is called, it will in turn call dispose() on all the objects submitted for disposal,
in the order opposite to the order of submission. There is also a disposable(Disposable) method which can
be called with any object, whether registered with the container or not.
This is the expected protocol for using the container:
dep(...) methods to populate the container. The objects being registered
are early-wired only.
wireAndInitializeAll(). This triggers late-wiring and initialization of all the
registered objects and promotes the container to the dep(...) calls can be made; now the objects are immediately late-wired and initialized. Also
initialize(Object) can be called to initialize an object without registering it with the container.
DiContainer constructor. It will be used
as a fallback container for any failed dependency lookup. The parent container is accessed in a strictly
read-only fashion.
| Constructor and Description |
|---|
DiContainer()
Creates an empty container.
|
DiContainer(DiContainer parent)
Creates an empty container which has a parent container.
|
| Modifier and Type | Method and Description |
|---|---|
DiContainer |
dep(Class<?> type)
Instantiates a type and registers the instance under that type.
|
<T> DiContainer |
dep(Class<? super T> declaredType,
Class<T> actualType)
Instantiates a type and registers the instance under the specified type.
|
<T> DiContainer |
dep(Class<? super T> declaredType,
T dep)
Registers an object by type.
|
DiContainer |
dep(Object dep)
Registers an object by its actual type.
|
DiContainer |
dep(String name,
Class<?> type)
Instantiates a type and registers the instance under the specified name.
|
<T> DiContainer |
dep(String name,
T dep)
Registers an object by name.
|
DiContainer |
disposable()
Adds the object registered by the preceding
dep(...) call to the list of objects to dispose
when this container is disposed. |
DiContainer |
disposable(com.hazelcast.nio.Disposable target)
Adds an object to the list of objects to dispose when this container is disposed.
|
void |
dispose()
Calls
dispose() on all objects submitted for disposal. |
<T> T |
get(Class<T> t)
Looks up a registered object by type.
|
Object |
get(String name)
Looks up a registered object by name.
|
<T> T |
get(String name,
Class<T> type)
Looks up a registered object by name and casts it into the supplied type.
|
void |
initialize(Object target)
Calls all
Initialize-annotated methods on the supplied object. |
<T> T |
instantiate(Class<T> type)
Instantiates the given type by calling its
@Inject-annotated constructor and
satisfying the constructor's dependencies. |
Object |
invoke(Object target,
String methodName) |
<T> T |
wire(T target)
Late-wires the provided target object without registering it with the container.
|
DiContainer |
wireAndInitializeAll()
Late-wires all the objects registered with the container, then calls all
Initialize-annotated methods
on each object. |
public DiContainer()
public DiContainer(DiContainer parent)
public DiContainer dep(Object dep)
dep - the object to registerthispublic <T> DiContainer dep(Class<? super T> declaredType, T dep)
declaredType - the type under which to register the objectdep - the object to registerthispublic <T> DiContainer dep(String name, T dep)
name - the name under which to register the objectdep - the object to registerthispublic DiContainer dep(Class<?> type)
type - the type to instantiatethispublic <T> DiContainer dep(Class<? super T> declaredType, Class<T> actualType)
declaredType - the type under which to register the instanceactualType - the type to instantiatethispublic DiContainer dep(String name, Class<?> type)
name - the name under which to register the instancetype - the type to instantiatethispublic DiContainer disposable()
dep(...) call to the list of objects to dispose
when this container is disposed.thisClassCastException - if the registered object does not implement Disposable.public DiContainer disposable(com.hazelcast.nio.Disposable target)
thispublic <T> T instantiate(Class<T> type)
@Inject-annotated constructor and
satisfying the constructor's dependencies. Does not register the instance with the container
and does not late-wire the instance.type - the type to instantiate.public <T> T wire(T target)
target - the target into which to inject its dependencies.targetpublic void initialize(Object target)
Initialize-annotated methods on the supplied object.public DiContainer wireAndInitializeAll()
Initialize-annotated methods
on each object. Objects are wired and initialized in the order in which they were registered.
It is an error to call this method on an already initialized container.
thispublic <T> T get(Class<T> t)
t - the type to look upnull if none foundpublic Object get(String name)
name - the name to look upnull if none foundpublic <T> T get(String name, Class<T> type)
name - the name to look upnull if none foundClassCastException - if an object is found, but doesn't comply with the supplied typepublic void dispose()
dispose() on all objects submitted for disposal. Objects are disposed in the order
opposite to the order in which they were submitted.dispose in interface com.hazelcast.nio.DisposableCopyright © 2021 Hazelcast, Inc.. All Rights Reserved.