jopen 7年前


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.