lecousin.net - Java framework - Injection
This library provides dependency injection functionalities.
It depends on the net.lecousin.core library mainly for parsing configuration files.
An object to inject can be a singleton, or using a factory. When using a factory, a new object is created by the factory each time it needs to be injected.
When an object needs to be injected, it can be injected
- by ID: by specifying the ID of the object or the factory
- by type: we look for an object matching the given type, and inject it
In addition, conditions can be given to specify when an injectable object (or factory) is eligible, based on properties. For example, a production implementation can be injected when a property is set to 'PROD', else a mock implementation can be injected.
An injectable object can also declare an initialization method to be called once it is instantiated and its dependencies have been injected if any.
Injection is done in an Injection Context, a context being composed of:
- Properties, that can be used in a configuration file or in the @InjectableWhen annotation
- Singletons, which are instances of objects ready to be injected
- Factories, which can be used to create an instance each time an object needs to be injected
- An optional parent context. If it has a parent, a context inherits from its content.