Dagger2 - creating a typesafe dependency injection graph at compilation



Dagger2 uses the class javax.inject.Inject to annotate the constructors and fields that need dependencies. The annotation class dagger.Provides is used to describe a method that can satisfy a dependency. The resulting @Inject and @Provides annotated classes form a graph of objects that can be validated at compile time.

Reflection or dynamic byte code is not utilised instead the generated source code is fully traceable.

Annotation processor support is used to generate the dependency graph Plugging into the Java Compiler.

Simple Java and Gradle example