If the construction parameter is a variable when instantiating a class, and this variable points to an object of the instantiated class. So is dependency injection used? Is my understanding correct?
In ordinary Java development, when programmers need to rely on methods of other classes in a certain class, they usually use new to depend on the class and then call the method of the class instance. The problem with this kind of development is that new class instances are not easy to manage uniformly. , spring proposed the idea of dependency injection, that is, dependent classes are not instantiated by programmers, but the spring container helps us specify new instances and inject the instances into classes that require the object. Another term for dependency injection is "inversion of control". The popular understanding is: usually when we create a new instance, the control of this instance is our programmers, and inversion of control means that the work of the new instance is not done by our programmers. Instead, leave it to the spring container.
That’s right. The first sentence in your question, 如果实例化一个类时构造参数是个变量,而这个变量指向一个已实例化的类的对象。means to inject dependencies through the constructor. In addition to the constructor method, there are also several methods mentioned by @Qiu Kangsingasong.
Excerpted from Spring Practice
In ordinary Java development, when programmers need to rely on methods of other classes in a certain class, they usually use new to depend on the class and then call the method of the class instance. The problem with this kind of development is that new class instances are not easy to manage uniformly. , spring proposed the idea of dependency injection, that is, dependent classes are not instantiated by programmers, but the spring container helps us specify new instances and inject the instances into classes that require the object. Another term for dependency injection is "inversion of control". The popular understanding is: usually when we create a new instance, the control of this instance is our programmers, and inversion of control means that the work of the new instance is not done by our programmers. Instead, leave it to the spring container.
That’s right. The first sentence in your question,
如果实例化一个类时构造参数是个变量,而这个变量指向一个已实例化的类的对象。
means to inject dependencies through the constructor. In addition to the constructor method, there are also several methods mentioned by @Qiu Kangsingasong.