Dependency Injection, be it Spring IoC, CDI or Google GUICE is often seen as hard to understand or even referred to as “black magic”.
As with all complex frameworks the actual working basis is fairly simple and straight forward. In this series I am going to demonstrate, in a rather simplified manner, the internal workings of dependency injection frameworks by developing one from scratch, Voodoo DI.
Please note, Voodoo DI is meant SOLELY for educational purposes, if you are looking for production ready DI frameworks please look at CDI, Spring IoC or Google GUICE.
In this series I am going to concentrate on the annotation driven approach based on JSR-330 Dependency Injection for Java annotations used by all major DI frameworks.
JSR-330 Dependency Injection for Java specifies a common set of annotations that are used by Spring, Guice and CDI.
- @Inject – defines injection points on fields, methods and constructors.
- @Qualifier – A qualifier may annotate an injectable field or parameter and, combined with the type, identify the implementation to inject.
- @Named – Similar to @Qualifier, it allows the identification of the correct implementation based on name and type.
- @Singleton – Singleton scope, only one instance is created by the DI framework.
- @Scoped – Used to define new scope annotations. The DI Framework may use these to define new contexts (e.g. @RequestScoped).
Continue reading Understanding Dependency Injection – Part 1 IoC