Framework interview questions: talk about my understanding of Spring IOC and DI

IOC is a design idea called "Inversion of Control".

1. A shallower level - from the name, 
"control" refers to the control of the life cycle of object creation, maintenance, destruction, etc. This process is generally actively controlled by our program, such as using the new keyword to Create an object (create), keep a reference (maintenance) during use, and recycle the object (destroy) by the GC after losing all references. 
" Inversion" means that the control of the life cycle of object creation, maintenance, and destruction is changed from program control to IOC container control. When an object is needed, it can be obtained directly from the IOC container by name.

2. A deeper level - when it comes to DI, dependency injection is an important implementation of IOC.  The creation of
an object often involves the creation of other objects. For example, a member variable of an object A holds a reference to another object B. , this is the dependency, A depends on B. Since the IOC mechanism is responsible for the creation of objects, this dependency must be taken care of by the IOC container. The responsible way is DI - dependency injection, by writing dependencies into configuration files, and then when creating objects with dependencies, the IOC container injects the dependent objects. For example, when creating A, it checks that there is a dependency, IOC The container creates the object B that A depends on and injects it into A (assembly, implemented through reflection mechanism), and then returns A to the object requester to complete the work.

3. What is the significance of IOC? 
IOC does not implement more functions, but its existence allows us to obtain suitable objects from the IOC container without a lot of code and without considering the complex coupling relationship between objects, and provides reliable management of objects, Greatly reduces the complexity of development.

Guess you like

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