public class Injector extends Object
Contains the micro kernel which keeps track of all parts and injects them where required. We consider this a micro kernel, as it doesn't directly know any annotations. It rather delegates the work to classes which implement defined interfaces. Therefore the implementation is rather short but easily extensible.
Parts are commonly added via subclasses of ClassLoadAction
. These scan each class in the classpath
and instantiate and insert them into the MutableGlobalContext
if required. Most of these
ClassLoadAction implementations trigger on annotations. A prominent example is the
AutoRegisterAction
which loads all classes wearing the .Register
annotation. Subclasses of ClassLoadAction are discovered automatically.
Accessing parts can be done in two ways. First, one can access the current GlobalContext
via
context()
. This can be used to retrieve parts by class or by class and name. The second way
to access parts is to use marker annotations like Part
,
Parts
. Again, these annotations are not processed by the micro kernel itself, but by
subclasses of FieldAnnotationProcessor
.
To process all annotations of a given Java object, GlobalContext.wire(Object)
can be used. This will
be automatically called for each part which is auto-instantiated by a ClassLoadAction.
Also all annotations on static fields are processed on system startup. This is a simple trick to pass a part to objects which are frequently created and destroyed.
Modifier and Type | Field and Description |
---|---|
static Log |
LOG
Logger used by the injection framework.
|
Modifier and Type | Method and Description |
---|---|
static GlobalContext |
context()
Provides access to the global context, containing all parts
This can also be loaded into a class field using the
Part annotation and
GlobalContext as field type. |
static List<Class<?>> |
getAllLoadedClasses()
Returns a list of all loaded classes.
|
static void |
init(Classpath classpath)
Initializes the framework.
|
public static final Log LOG
public static void init(@Nonnull Classpath classpath)
This is automatically invoked by Sirius.start(sirius.kernel.Setup)
.
classpath
- the classpath used to enumerate all classes to be scannedpublic static GlobalContext context()
This can also be loaded into a class field using the Part
annotation and
GlobalContext as field type.
Copyright © 2018. All rights reserved.