In Java development interviews, you are often asked what Spring IOC is. Understanding the Spring IOC design pattern is also helpful in troubleshooting problems during work development. Today’s article begins with an easy-to-understand introduction to Spring IOC.
Spring IOC and DI
Spring IOC (Inversion of Control) and DI (dependency injection) are introduced in a simple and easy way. I also sorted it into my own knowledge, convenient for myself to understand and communicate with others.
It’s an IOC Inversion Of Control. In traditional Java SE programming, we create objects directly inside the object by new method, and the program actively creates dependent objects. In Spring programming, IOC has a special container for controlling objects.
Control is the creation, initialization, and destruction of objects.
- Create object: the original new object is now created by the Spring container.
- Initializing objects: Objects that used to assign values to dependent objects via constructors or setter methods are now automatically injected by the Spring container.
- Destroying objects: Instead of assigning null to objects or doing some destruction, it is now the responsibility of the Spring container management life cycle to destroy objects.
Conclusion: IOC solves the tedious operation of the object lifecycle and decouples our code.
So-called inversion:
In fact, the control is reversed. As mentioned earlier, Spring controls the life cycle of the object, so the control of the object is completely taken out of our control and given to Spring. This reversal is where we go from being object controllers to passive IOC controllers.
What can the IOC do?
The IOC container solves the coupling problem perfectly, allowing even unrelated objects to have injection relationships.
In IOC mode, you just design good processes and dependencies, define what is required, and then hand over control to Spring.
DI: Dependency injection.
Dependency injection is an implementation, whereas IOC is a design idea. From IOC to DI is from theory to practice. The program hands the dependency to the container, and the container manages the dependency for you, which is the heart of dependency injection.
Benefits: Dependency injection reduces development cost, improves code reuse rate and software flexibility.
Who depends on whom and why; Who injected whom and what:
- Who depends on whom: The A object depends on the IOC container.
- Why it needs dependency: Object A needs the IOC container to provide external resources such as data and object B, without which business processing cannot be completed.
- Who injects whom: The IOC container injects A objects.
- What is injected: The IOC container injects the data required by object A, external resources such as object B, on demand.
Relationship between IOC and DI:
It’s a different way of describing the same concept, but it’s actually different. IOC emphasizes the reversal of control of the container and the object, while DI emphasizes the injection of the object’s dependencies by the container. In a broad sense, IOC is a development pattern, and DI is one of the implementation methods, which can be understood as: the use of dependency injection to achieve inversion of control. Spring has chosen DI to make it popular in Java development.
Second, the summary
IOC: It’s a design idea. In Spring development, the IOC container controls object creation, initialization, destruction, and so on. This reverses control of objects from our control to Spring IOC’s control of objects. IOC decouples code, allowing even unrelated objects to have injection relationships.
DI: is the concrete implementation of IOC. The program hands the dependency to the container, and the container manages the dependency for you, which is the heart of dependency injection. You need to understand who depends on whom and why; Who injected whom, what was injected and so on.
IOC emphasizes the reversal of control of the container and the object, while DI emphasizes the injection of the object’s dependencies by the container.