I saw the following example on the Dagger 2 website:
class Thermosiphon implements Pump {
private final Heater heater;
@Inject
Thermosiphon(Heater heater) {
this.heater = heater;
}
...
}
and the documentation:
When a new instance is requested, Dagger will obtain the required
parameters values and invoke this constructor.
When I write a Module to provide a Thermosiphon
like
@Module
public class ThermosiphonModule {
@Provides
@Singleton
Thermosiphon provideThermosiphon() {
return new Thermosiphon(???);
}
}
the Thermosiphon
constructor still requires a Heater
as an argument, rendering the whole 'automatic injection of constructor dependencies' useless.
I tried
return new Thermosiphon(null);
and
return new Thermosiphon();
(empty constructor) and hoped for Dagger2 to pick up that I wanted the missing Heater
to be injected, yet the Heater of the provided Thermosiphon
is always null;
I verified though my HeaterComponent
/ HeaterModule
are working fine and are able to provide a Heater
.
Do I completey misunderstand the whole feature of 'Dagger satisfies constructor dependencies for you' or am I missing something?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…