Android和Java的依赖注入器:Dagger

jopen 7年前

Dagger是一个快速的Android和Java依赖注入器。

Use @Inject to annotate the constructor that Dagger should use to create instances of a class. When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.

class Thermosiphon implements Pump {    private final Heater heater;      @Inject    Thermosiphon(Heater heater) {      this.heater = heater;    }      ...  }

Dagger can inject fields directly. In this example it obtains a Heater instance for the heater field and a Pump instance for the pump field.

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

If your class has @Inject-annotated fields but no @Inject-annotated constructor, Dagger will use a no-argument constructor if it exists. Classes that lack @Inject annotations cannot be constructed by Dagger.

Dagger does not support method injection.

项目主页:http://www.open-open.com/lib/view/home/1388224294328