Spring's inversion of control (IOC) and Dependency Injection (DI) detailed
The following (IOC) control inversion is introduced first:
The so-called control inversion is that the application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects is the responsibility of the external container. So that the controller has been transferred to the external container, the transfer of control is reversed
The sample code is as follows:
public class personservicebean{ private Persondao Persondao = new <strong><span style= "color: #FF0000;" >PersonDaoImpl</span></strong> (); public void Save (person person) {Persondao.save (person);} }
Persondaoimpl is created and maintained internally within the app. The creation of an object is externally responsible. This is an IOC.
(DI) Dependency Injection
Dependency Injection is: During a program run, there is an external container that dynamically injects dependent objects into the component.
When we give the dependent object to the external container for creation, the Personservicebean class can be changed to the following:
public class personservicebean{ private Persondao Persondao; By constructor parameter injection //can also be injected via setter method public Personservicebean (Persondao persondao) { This.persondao = Persondao; } public void Save (person person) { persondao.save (person);} }
Di is generated on the basis of the IOC.
Spring's inversion of control (IOC) and Dependency Injection (DI) detailed