Java – dependency injection: should dependency objects be passed as constructor args?

Video here

It explains what it should be

How to explain dependency injection to a 5-year-old?

So I'm confused now

Solution

I personally prefer the fact that constructors can naturally define all required fields, and those that have been set cannot be changed You can do the same with setters, but there is no direct language support

Although I prefer to use constructors for mandatory and final fields, I don't think this is specific to dependency injection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>