believe that contact with spring students, for dependency injection is not unfamiliar.
When I first heard about the name, I didn't understand what it was called Dependency injection, but later found that dependency injection has always been in our daily code, but we did not deliberately put it forward, and then take such a name.
At first, when defining a class, it often relies on other classes, such as the spelling checker, which relies on dictionaries:
As a tool class, we are generally declared as static tool classes:
Public class Spellchecker {
Private static final englishdic dictionary=new Lexicon ();
//Prohibit instantiation Private spellchecker () {} Public Static boolean isValid (String word) {} Public Static List<string> Suggestions (String typo) {}}
or define Spellchecker as a singleton:
Public class Spellchecker {
Private final Englishdic dictionary = ...; Private spellchecker (...) {}
Public Static New spellchecker (...); Public boolean isValid (String word) {...}
Public List<string> Suggestions (String typo) {...}}
Can see that we are dependent on the Englishdic class, if this time the project has been completed and issued a version, this time the boss said that a German customer wants to use our library, we hope that we support the German language. At this time, we had to modify the code to change the Englishdic to Germandic, and then to send a version.
Later, as the business expands and grows, it needs to support a variety of languages, and if it still exists in this way, then we will maintain dozens of versions. This time, smart people will think, dictionary is not at the beginning of the definition, but as a mutable attribute, when the user used to pass a Dictionary object, so that we can only maintain a version.
Now we have two choices.
One is to add a setter. (This is not possible, of course, because you can't guarantee when the user will call the setter, and it's awkward)
In fact, we can see that the above requirements have changed, because later requirements have become the custom dictionary attributes that each user wants to have, so we discard the static tool class settings and use Dependency injection :
// Dependency Injection provides flexibility and testability Public class spellchecker { private final Lexicon dictionary; Public spellchecker (Lexicon dictionary) { this. Dictionary= Objects.requirenonnull ( dictionary); } Public boolean isValid (String word) {...} Public List<string> Suggestions (String typo) {...}}
So the customer needs to use what dictionary is the customer's own decision, and our code does not need to maintain multiple copies.
The popular explanation for dependency injection is that the object that the class relies on is used as a parameter to inject the user into the call.
This will make the code more flexible.
When does dependency injection work?
When a class relies on another mutable class, the mutable class should be initialized with an injected method.
In reality, a class may depend on objects of many classes. This time it can be built in combination with builder mode.
Or: Spring to understand.
"Effective Java" reading notes (v) using dependency injection instead of the original resource dependency