Mid-Autumn Festival Creative Submission Contest Q&A

Those who have studied the Spring framework must have heard of the two concepts of IoC(Inversion of Control) and DI(dependency injection). For those who are new to Spring, the concepts of IoC and DI are always vague and difficult to understand. Today, I would like to share with you my understanding of Spring IOC and the understanding of some technical leaders on the Internet about Spring framework IOC.

1.1. What is IoC

** Inversion of Control is not an Inversion of technology. It’s a design idea. ** In Java development, **Ioc means handing your designed objects over to the container for control, rather than the traditional direct control within your objects. How does ** understand Ioc well? The key to a good understanding of Ioc is to be clear about “who controls whom, what controls, why there is a reversal (there should be a positive reversal), which aspects of the reversal”. Then let’s have an in-depth analysis:

** Who controls who, controls what: ** Traditional Java SE programming, we directly create objects inside the object through new, the program is active to create dependent objects; IoC has a special container to create these objects, that is, the IoC container controls the creation of objects; Who controls whom? The IoC container, of course, controls the objects; Control what? That is, it mainly controls the acquisition of external resources (not just objects including files, etc.).

** There is inversion, there is forward, traditional applications are by our own active control in the object to directly obtain dependent objects, that is, forward; In inversion, the container helps create and inject dependent objects. Why reverse? Because the container helps us find and inject the dependent object, the object only passively accepts the dependent object, so it is inversion; What has been reversed? The retrieval of dependent objects has been reversed.

To illustrate, traditional programming, as shown in Figure 2-1, takes the initiative to create related objects and then combine them:

When you have an IoC/DI container, you no longer actively create these objects in the client class, as shown in Figure 2-2:

1.2. What can IoC do

IoC is not a technology, just an idea, an important object-oriented programming discipline that can guide us in designing loose-coupled, better programs. In traditional applications, we actively create dependent objects within classes, which leads to high coupling between classes and is difficult to test. With the IoC container, the control of creating and finding dependent objects is given to the container, which injects composite objects. Therefore, objects are loosely coupled between objects, which facilitates testing, functional reuse, and more importantly, makes the overall architecture of the program very flexible.

In fact, the biggest change IoC has brought to programming is not in the code, but in the mind. The application used to be the boss, taking the initiative to acquire whatever resources it wanted, but in IoC/DI thinking, the application became passive, waiting for the IoC container to create and inject the resources it needed.

The IoC embodies one of the laws of object-oriented design — the Hollywood law: “Don’t come to us, we’ll come to you.” That is, the IoC container helps the object to find the corresponding dependent object and inject, rather than the object to find.

1.3 IoC and DI

DI – Dependency Injection: The dependencies between components are determined by the container at runtime. Figuratively, the container dynamically injects a Dependency into the component. The purpose of dependency injection is not to bring more functions to the software system, but to increase the frequency of component reuse and build a flexible and extensible platform for the system. ** Through the dependency injection mechanism, we can specify the resources needed by the target through simple configuration, without any code, and complete our business logic, regardless of where the specific resources come from and who implements them.

The key to understanding DI is: “Who depends on whom, why depends on whom, who infuses whom, what infuses”. Let’s take a closer look:

● Who depends on whom: Applications depend on the IoC container, of course;

●** Why dependencies: ** Applications need IoC containers to provide external resources that objects need;

● Who injects whom: It is obvious that the IoC container injects an application object, an object that the application depends on;

● What is injected: It is the external resources (including objects, resources, constant data) needed to inject an object.

What is the relationship between IoC and DI? In fact, they are different descriptions of the same concept. Since the concept of inversion of control is rather vague (perhaps only understood as the level of the container controlling objects, it is difficult to think of who maintains the object relationship), Martin Fowler, a master of science, gave a new name in 2004: “Dependency injection”, in contrast to IoC, dependency injection explicitly describes how “injected objects depend on IoC containers to configure dependent objects”.

I have read a lot of articles on the understanding of Spring’s Ioc. Many people’s explanations of Ioc and DI are obscure and difficult to understand. Anyway, I have a feeling that I cannot explain and understand them. He explains every word of IoC(Inversion of control) and DI(dependency injection) very clearly, and when he reads it, it feels like a revelation. I believe it will be of great help to those who are new to the Spring framework to understand Ioc.

2.1 IoC(Inversion of Control)

I’d like to start with IoC. This is at the heart of Spring, throughout. ** IoC, for the Spring framework, means that Spring is responsible for controlling the life cycle of objects and their relationships. ** What does that mean, to take a simple example, how do we find a girlfriend? The common situation is that we go around to see where there are beautiful and nice MMS, and then ask about their interests, QQ number, telephone number, IP number, IQ number… Find a way to get to know them, give them what they want, and heh heh… The process is complex and profound, and we have to design and face each step ourselves. In traditional program development, if you want to use another object in an object, you have to get it (new one yourself, or look it up from JNDI) and then destroy the object (Connection, etc.). The object is always coupled with other interfaces or classes.

So what does the IoC do? It was a bit like finding a girlfriend through a matchmaking agency, which introduced a third party between me and my girlfriend: the matchmaking agency. Matchmaking management a lot of data of men and women, I can put forward to dating a list, telling it what I want to find a girlfriend, look like michelle reis, for example, figure like Lin Xilei, singing like jay Chou, speed technology like zinedine zidane, like carlos, then dating would, according to the requirements of our provide a mm, We just need to fall in love with her and get married. Plain and simple, if the matchmaker gives us someone who doesn’t meet our criteria, we throw an exception. The whole process is no longer controlled by me, but controlled by the matchmaking agency, which is like a vessel. That’s what Spring is all about. All the classes register in the Spring container, tell Spring what you are and what you need, and Spring will give you what you want when the system is running properly, and hand you over to other things that need you. The creation and destruction of all classes is controlled by Spring, which means that it is not the reference object that controls the life cycle of an object, but Spring. For a specific object, it used to control other objects, but now all objects are controlled by Spring, so this is called inversion of control.

2.2 DI(Dependency Injection)

One focus of IoC is to dynamically provide one object with the other objects it needs while the system is running. This is achieved through DI (Dependency Injection). For example, object A needs to operate on the database. In the past, we always had to write code in A to get A Connection object. With Spring, we just need to tell Spring that A needs A Connection. When the system is running, Spring will make A Connection at the appropriate time and inject it into A like A needle, thus completing control over the relationships between objects. A relies on Connection to function properly, and this Connection is injected into A by Spring, hence the dependency injection name. So how is DI implemented? One of the most important features since Java 1.3 is Reflection, which allows programs to dynamically generate objects, execute object methods, and change object properties at runtime. Spring uses reflection to implement injection.

Once you understand the concepts of IoC and DI, everything becomes straightforward, and all that is left is to stack the wood in the Spring framework.

3. My understanding of IoC(Inversion of Control) and DI(dependency injection

In normal Java application development, we need at least two or more objects to cooperate to achieve a certain function or to complete a certain business logic. When Spring is not used, each object needs to use its partner object. We all need to use syntax like New Object () to create the cooperative object. This cooperative object is created by ourselves. The initiative to create the cooperative object is in our hands, and the initiative and creation time of the cooperative object are controlled by ourselves. In this way, the degree of coupling between objects will be high. Object A needs to use the cooperative object B to accomplish one thing together. If A wants to use B, then A will be dependent on B, that is, there is A coupling relationship between A and B, and they are tightly coupled together. Spring creates the B object and stores it in A container. When A needs to use the B object, Spring takes the B object that A wants to use from the container and gives it to A. How Spring creates the object? Object A doesn’t need to care about these details (when you were born and how you were born I don’t care, just help me with my work). After A gets the object that Spring gives us, the two of us can work together to complete the work to be completed.

Inversion of Control refers to the transfer of Control of object creation. In the past, the initiative and timing of object creation were controlled by the IoC, but now the power is transferred to a third party, such as the IoC container, which is a factory for object creation. It just gives you objects, and with the IoC container, the dependencies change, the old dependencies go away, they all depend on the IoC container, and they’re all connected through the IoC container.

This is my understanding of Spring’s IoC**(Inversion of control). DI(Dependency injection)** is just another word for IOC, and DI was first proposed by Martin Fowler in a paper in early 2004. He concludes: What control has been reversed? Namely: the way of obtaining dependent objects has been reversed.

4, summary

As for the core concept of Spring Ioc, I believe that everyone who studies Spring will have their own understanding. There is no absolute standard answer to this conceptual understanding.