First of all, read the Coinjema Dependencies Document for how to setup your project to use Coinjema.
For Coinjema to work, it must be initialized with information about where to find context configuration. This means making a call to:
To write a class that will be handled by Coinjema, use the @CoinjemaObject class-level annotation:
@CoinjemaObject public class ExampleClass { public ExampleClass() { .. } }
When the above ExampleClass is instantiated, Coinjema will intercept the constructor call and will look for methods that are annotated with @CoinjemaDependency. These methods are the means by which Coinjema hands your object its dependencies. For instance:
@CoinjemaObject public class ExampleClass { Log log; public ExampleClass() { .. } @CoinjemaDependency public void setLogger(Log l) { log = l; } }
Coinjema will search the current context for a file that matches the setLogger(Log) method. It goes through a series of possibilities that essentially move from more specific to more general. An outline of the steps is:
- First, find the already created Log instance for ExampleClass in the current context. Coinjema only runs the configuration scripts once and caches the result for the class.
- Look for a file named ExampleClass.setLogger.*** and "evaluate"
it to a Java object. *** represents a file extension that Coinjema understands.
Out-of-the-box, Coinjema understands .groovy, .groovyClass, .properties, .txt, .jser, .compmap, and .redirect (for
groovy script, groovy class, and properties, plain text, serialized java classes, component sets, and
redirection files respectively). More can be
registered with Coinjema as implementations of the Evaluator
interface.
To summarize, Coinjema will look for files named
ExampleClass.setLogger.groovy,
ExampleClass.setLogger.groovyClass
ExampleClass.setLogger.properties
etc... - If none are found, Coinjema looks for a cached object of the type of the injected class (Log, in our case). This would represent a "shared dependency" (Coinjema's version of a singleton).
- If no such shared dependency can be found in memory, Coinjema searches again for a file named Log.***.
- If still no dependency is found, repeat the entire process for the parent context of the current context. In other words, your configuration contexts inherit their settings from each other.
- If no dependency found, Coinjema will throw an runtime exception.