In-depth understanding of Inversion of Control and Dependency Injection in Spring

1. Inversion of Control (IOC: Invocatoin of controll): Inversion of control is not a technology, but a design idea . The traditional creation of objects is to instantiate them directly in the code like Person person = new Person(). In Spring there is a container called the IOC container, which is specially used to manage the creation of objects. When using the Spring framework, because operations such as Person person = new Person() are directly handed over to the IOC container for implementation, we can directly use the instance of the Person object. 2. Dependency Injection (DI: Dependency Injection): During the running of the program, the IOC container dynamically injects the dynamic relationship into the component . For example, the Person (user) object needs to call the PersonDetail object (user detail list). In the traditional method, the Person object must first instantiate the PersonDetail object, and then the person object can use the methods in the PersonDetail object. After using the IOC container, when the Person needs to use the PersonDetail object, the IOC container will inject the instance of PersonDetail into the Person object through dynamic injection, so that we do not need to instantiate the PersonDetail object. 3. The difference between Inversion of Control and Dependency Injection: They talk about a concept, but describe it from different aspects. Inversion of control is easy to understand as the creation of IOC container control objects, without a very specific description of how the relationship between objects is maintained. In order to distinguish this concept, Martin Fowler gave a new definition of "dependency injection" in 2004. "Dependency injection" describes that objects rely on the IOC container to manage the relationship between objects.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119169&siteId=291194637