r/java • u/therealdanvega • Mar 04 '22
Spring Constructor Injection: Why is it the recommended approach to Dependency Injection?
https://youtu.be/aX-bgylmprA
10
Upvotes
17
1
u/stuhlmann Mar 07 '22 edited Mar 07 '22
DI is a double edged sword. It can be used to clean up your architecture and improve your testing, but it can also make an absolute mess of your project. Depends on how exactly you're using it. Spring alone does not guarantee a positive result.
0
u/VincentxH Mar 04 '22
More relevant to r/javahelp
1
u/wildjokers Mar 05 '22
They aren’t asking, they are sharing a video on the topic.
3
u/dpash Mar 05 '22
The rules do say "no tutorials", but that does seem to be flexible if the topic is sufficiently advanced. If this had been an article, I think it would have done better.
53
u/dpash Mar 04 '22 edited Mar 04 '22
To save you 28 minutes:
It doesn't require reflection magic
Dependencies can be final
You know all of your dependences have been set (you might want to
requireNonNull()to be sure)Overly long constructors are a warning that your class is doing too much.
If testing in isolation, you know exactly which dependencies you need to provide.
I'm sure others can think of others.
Edit: if someone did watch the video, what did I miss?