The Future of Dependency Injection with Dagger 2
Why Stack Overflow recommends it
Stack Overflow users link it as the clearest explanation of how Dagger 2 works under the hood, including how components, modules and @Inject fit together.
Recommended by 5 people across 5 different months, 2016 to 2022.
What it is
A 54-minute conference talk by Jake Wharton on Dagger 2, the compile-time dependency injector for Java and Android. It briefly introduces dependency injection, then walks through the new Dagger 2 concepts and APIs with real-world examples and some advanced tricks.
What people said
Strong rec TheLibrarian · 2022-10 · Stack Overflow · on “How does Hilt inject ViewModel parameters”
How do you tell the Hilt ? With annotations . There is one REALLY good presentation from Jake Wharton [this video] on how Dependency injection works in Dagger2 (on which Hilt is based on, so the idea is the same). Most of what Dagger2/Hilt/DI is a Service Locator [LINK] . Hilt is a compile-time thing so it goes over all of your files with those annotations and takes note of what is provided where and what needs what and generates files that do all of that logic "under the hood". Even in your example: @HiltViewModel class ExampleViewModel @Inject constructor( private val savedStateHandle: SavedStateHandle, private val repository: ExampleRepository ) : ViewModel() { ... } You tell Hilt that Example
Recommends Tudor · 2016-03 · Stack Overflow · on “Constructor injection Dagger 2”
I'm not sure either how this should work. [this video] in this video he removes the last @Provide and it should work. I will try tomorrow that example
Recommends Liam · 2019-03 · Stack Overflow · on “Dagger 2 Android - inject() dependencies into ViewModels vs Application with dependency reference(s)”
I am creating a basic Android application with Dagger 2. I was having a lot of difficulty understanding how to use it properly until I came across this great talk by Jake Wharton [this video] . In it, he demonstrates using Dagger 2 with a "Tweeter" app. At ~22:44 [this video] , he shows that an app's @Inject fields can be satisfied with an inject method. He later shows a simple Android implementation of this. My app's ViewModels rely on a repository class. I'm using Dagger 2 to inject this repository into the ViewModels, through the Application class, like this: //In my Dagger 2 component @Singleton @Component(module = {MyRepositoryModule.class}) public interface MyRepositoryComponent{ void inject(MyV
Recommends Thorben Kuck · 2019-08 · Stack Overflow · on “Is static metaprogramming possible in Java?”
The short answer This question is nearly more than 10 years old, but I am still missing one answer to this. And this is: yes , but not because of generics and note quite the same as C++. As of Java 6, we have the pluggable annotation processing api [LINK] . Static metaprogramming is (as you already stated in your question) compile-time program execution If you know about metaprogramming, then you also know that this is not really true, but for the sake of simplicity, we will use this. Please look here [LINK] if you want to learn more about metaprogramming in general. The pluggable annotation processing api is called by the compiler, right after the .java files are read but before the compile
Recommends Sarang · 2020-11 · Stack Overflow · on “Dagger with Java AWS Lambda?”
No need to provide 0-arg-constructor. Try lambda event and extend request handler. guice, spring resolve the dependency on runtime, which might increase your cold start time. On the other hand, dagger2 resolves dependency on compile time. For more details: [this video] [LINK]
Cites Spidey · 2016-08 · Stack Overflow · on “Simplest way to create a Singleton w/ Dagger 2?”
This is a simplified (not completely precise -- check links below) text gradually explaining Dagger2 and the idea behind it. My hopes are that you'll be able to read and understand other details on Dagger2 after this text. Don't focus on the term singleton. Dagger2 (forget dagger and use dagger2) exists to enforce object scope. Not the scope we usually talk about (classes, methods, loops) but the scope on an architectural level (you define those layers). Some typical layers in Android are Application, Activity and Fragment. As you know, your android app gets only one instance of Application class. Many instances of Activity class and many instances of Fragment class. You want to keep your ap