Is it possible to inject into supertypes in Dagger 2?
if I have a property like this
@Inject
Wallet<Material>
Would the following provide work ?
@Provides
Wallet<LeatherMaterial> provide()
{
return new Wallet<LeatherMaterial>
}
Actually I should have rephrased the question . It isn't working and I get an error that demands an injection of the exact Wallet
do we have any workarounds ? . does koin provide some functionality like this?
Yes, you need to be explicit. Add the following to your Module, if you already have Wallet<LeatherMaterial>
in your dependency graph somewhere:
@Binds abstract Wallet<Material> provide(Wallet<LeatherMaterial> leatherWaller);
Otherwise, go with:
@Provides static Wallet<Material> provide() {
return new Wallet<LeatherMaterial>(){ /* ... */ };
}
Edit: Revisiting this answer because it occured to me the solutions I've provided wont work due to how generics are treated in Java. Instead, you would need to use:
Wallet<? extends Material>
Wallet<? extends Material>
in place of Wallet<Material>
in my above answers, and do the same wherever it is injected as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.