Back Home

Overview

Dagger was created after Guice, by one of Guice's creators (“Crazy Bob” Lee) after his move to Square:

  • Spring was originally released in October 2002.
  • Google originally publicly released Guice in March 2007.
  • JSR-330 formalized javax.inject annotations in October 2009, with heavy input from Google (Bob Lee), Spring, and other industry players.
  • Square originally released Dagger 1 publicly in May 2013.
  • Google originally released Dagger 2 publicly in April 2015.
  • Square marked Dagger 1 as deprecated on September 15, 2016. In that sense, the continued curation of Guice isn't “reinventing the wheel” so much as maintenance on a long-running and widely-consumed software package that thoroughly predates any version of Dagger. To list and amend to the differences you have above:

* Spring is a relatively-heavyweight framework with a lot of integrations, an XML configuration language, and runtime/reflective bindings. Applications already using Spring can use Spring's dependency injection framework with very little extra work.

  • Guice is a relatively-lightweight framework with fewer integrations, Java instance configuration, and runtime/reflective bindings. With the use of Java bindings, you get compile-time type checking and IDE autocomplete integration.
  • Dagger is a very lightweight framework with very few integrations, Java interface/annotation configuration, and compile-time code-generated bindings. The code generation aspect makes Dagger very performant overall and particularly in resource-limited and mobile environments. (Android's different VM makes reflection especially slow, so Dagger is especially useful here.)
  • All three of the above frameworks support JSR-330, so a well-crafted library or application can be mostly agnostic to the DI container used.

Spring

Spring Boot

Dagger2

Dependency injection's vision was that simple plain old java objects (pojos) could be created and tested stand alone without need for a large amount of dependencies.

Dagger 1 was able create an instance of the class and inject the objects into the defined dependencies via Java Reflections. This eliminated the hard dependencies. That way the class could be tested in isolation, ex. by using mock objects.

However reflection is slow in itself and second, it used to perform dependency resolution at runtime, leading to unexpected crashes, hence the need for a dagger2. Especially since one of the targets is resource constrained mobile (android) devices.

Dagger2 instead uses a dependency graph using annotation processor. The classes providing the dependencies were now generated at build time using javax inject package. This facilitated the possible error checking before the application runs. The generated code is highly readable as if written by hand.

  • @Component: selected modules are enabled to perform dependency injection.
  • @Inject To request a dependency in any class.
  • @provides and @module dependency provider class or functions.

Note unlike perhaps other frameworks:

  • Dagger2 does not inject fields automatically.
  • It cannot inject private fields.

Required dependencies:

  compile "com.google.dagger:dagger:2.8"
  annotationProcessor "com.google.dagger:dagger-compiler:2.8"
  provided 'javax.annotation:jsr250-api:1.0'
  compile 'javax.inject:javax.inject:1'

We tell dagger to inject classes and the type of classes to inject (from the dependency graph) with annotations

First we annotate the classes that “provide” classes with the @Provides annotation. The simplest version:

@Provides static Heater provideHeater() {
  return new ElectricHeater();
}

The methods that provide classes must be annotated with @Module e.g.

@Module
class DripCoffeeModule {
  @Provides static Heater provideHeater() {
    return new ElectricHeater();
  }
 
  @Provides static Pump providePump(Thermosiphon pump) {
    return pump;
  }
}

These exposed classes can then be injected into other classes that require them, either on the the variable declarations or constructors:

class CoffeeMaker {
  @Inject Heater heater;
  @Inject Pump pump;
 
  ...
}

Dagger2 and Kotlin

Guice

 
dependency_injection.txt · Last modified: 2017/12/14 03:41 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd