SpringIOC is the most important part of Spring Core, and to understand Inversion of Control, I feel it is important to understand an important idea of software design: dependency Inversion.

SpringIOC is the most important part of Spring Core, and to understand Inversion of Control, I feel it is important to understand an important idea of software design: dependency Inversion.

2. Abstraction should not depend on details. Details should depend on abstractions. 3. The central idea of dependency inversion is interface oriented programming. The dependency inversion principle is based on the design idea that abstract things are more stable than details. An architecture based on abstraction is much more stable than one based on detail. 5. The purpose of using interfaces or abstract classes is to specify specifications that do not involve any concrete actions, leaving the task of presenting the details to their implementation classes.

Dependency injection DI

Now suppose we need to design a suitcase that depends on the box, the box depends on the chassis, and the chassis depends on the wheels.

Now, assuming that the wheels need to be resized, the chassis needs to be changed, the box needs to be changed, the trunk needs to be changed. The code is as follows:

Now, if we need to change the size of the wheel to dynamically adjustable, then the upper layer code will have to change as well, so we can see that just to change the constructor of the wheel, this design needs to change the constructors of the entire upper class! In software engineering, the design is almost unmaintainable – in the actual engineering project, some classes may be thousands of bottom, if every time to modify this class, we have to modify all classes to it as a dependent, the software maintenance cost is too high, this is typical of the superstructure rely on base.

So we need inversion of control (IoC), where the top controls the bottom, not the bottom controls the top. We use Dependency Injection to achieve inversion of control. The so-called dependency injection is to pass the lower class as a parameter to the upper class to realize the control of the upper class to the lower class. Here we override the constructors of each class using dependency injection passed by the constructor:

How does dependency injection solve this problem? We can design the trunk first, the trunk according to the trunk, the chassis according to the chassis, and the wheels according to the chassis, as shown below:

Therefore, dependency injection is to pass the lower class as a parameter to the upper class to achieve the control of the lower class.

In this way, changes to the underlying classes do not affect changes to the upper classes, increasing the maintainability of the code. Here we do dependency injection by passing in the constructor. There are three other methods: Setter pass and interface pass and annotation. I won’t talk about it here, but the idea is the same, it’s all about inversion of control.

The relationship between IOC and DI and DL

EJB is IOC using DL, DI is the mainstream implementation of IOC today. DI provides Setter injection, interface injection, annotation injection, constructor injection and many other injection methods. How does the dependency inversion principle relate to IOC?

The dependency inversion principle, IOC, DI, and the Spring IOC container are related: The dependency inversion principle is an idea that high-level modules should not depend on low-level modules, but both should depend on their abstractions. It is under the guidance of inversion principle that IOC has the idea of inversion of control. How to realize this idea? Without support like dependency injection, Spring and other frameworks put forward the concept of containers based on IOC. For the IOC. The most important thing about a container is that it controls dependency injection (DI) for the lifecycle of container-managed beans. It is a container that holds objects, makes it easy to assemble objects, avoids using new to create classes everywhere, and can be maintained uniformly.

If we were to go to new step by step, we would see the situation above, which would need to be injected step by step according to the constructor:

The ICO container injection process is shown in the lower part of the figure, where the dependencies of the object are first looked up and then the injection is carried out from the bottom up.