From the course: Android Dependency Injection with Dagger 2 and Kotlin

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Method and field injection

Method and field injection

From the course: Android Dependency Injection with Dagger 2 and Kotlin

Start my 1-month free trial

Method and field injection

- [Instructor] As I've described earlier, once you understand what dependency injection is, and that Dagger is a compiler extension that auto-generates factory code, and you are comfortable with the at sign inject and at sign component annotations, you are pretty much ready to use Dagger in your application. The rest is just tricks for handling some special cases. So let's have a look at one of these special cases. The type of injection that we've seen so far is called constructor injection for obvious reasons. Dagger supports two other kinds of injection, method injection and field injection. Method and field injection should be used as a last resort. They're really not the best idea and you should use them only when absolutely necessary. To use method or setter injection, annotate the setter for a field with the at sign inject annotation. I'm going to do that now. First of all, I'm going to remove this constructor. I'm going to replace it with a var. Next, I'm going to initialize…

Contents