Detailed explanation of springIOC and DI (easy to understand)

This article is a reprinted article:
People who have studied Spring framework must have heard of Spring's IoC (inversion of control) and DI (dependency injection) concepts. For those who are new to Spring, they always feel that IoC and DI The concept is ambiguous and difficult to understand. Today, I will share with you some of the online technical experts' understanding of the IOC of the Spring framework and my understanding of the Spring Ioc.

1. Sharing the wonderful explanation of Ioc by Kaitao of
Iteye The first thing to share is Kaitao's understanding of the IOC of the Spring framework, which is written in a very straightforward manner. The following contents are all from the original text, the original address: http ://jinnianshilongnian.iteye.com/blog/1413846

1.1. What is IoC?

Ioc-Inversion of Control, that is, "inversion of control", is not a technology, but a design idea. In Java development, Ioc means handing over the object you designed to the container control, rather than the traditional direct control inside your object. How to understand Ioc well? The key to understanding Ioc is to clarify "who controls who, controls what, why is it reversed (if there is a reverse, there should be a forward rotation), and what aspects are reversed", then let's analyze it in depth:

Who controls who, controls what: In traditional Java SE programming, we create objects directly inside the object through new, and the program actively creates dependent objects; and IoC has a special container to create these objects, that is, the Ioc container to create these objects. Controls the creation of objects; who controls whom? Of course the IoC container controls the object; what? That is, it mainly controls the acquisition of external resources (not just objects including files, etc.).

Why is it reversed, and which aspects are reversed: if there is a reverse, there will be a forward rotation. The traditional application is actively controlled by ourselves in the object to directly obtain the dependent object, that is, the forward rotation; while the reverse rotation is caused by the container to help create and inject dependent objects; why reverse? Because the container helps us find and inject dependent objects, the object only passively accepts dependent objects, so it is reversed; which aspects are reversed? The acquisition of dependent objects is reversed.

To illustrate with a legend, the traditional programming as shown in Figure 2-1, is to actively create related objects and then combine them:

Figure 1-1 Schematic diagram of traditional application

When there is an IoC/DI container, these objects are no longer actively created in the client class, as shown in Figure 2-2:

Figure 1-2 Schematic diagram of program structure with IoC/DI container

1.2. What can IoC do?

IoC is not a technology, but an idea, an important principle of object-oriented programming, which can guide us how to design loosely coupled and better programs. In traditional applications, we actively create dependent objects inside the class, 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 handed over to the container. By injecting and combining objects, the objects are loosely coupled, which is also convenient for testing, which is conducive to functional reuse, and more importantly, makes the entire architecture of the program very flexible.

In fact, the biggest change that IoC brings to programming is not from the code, but from the idea, the change of "master-slave transposition" has taken place. The application is originally the boss, and it takes the initiative to acquire any resources, but in the IoC/DI idea, the application becomes passive, passively waiting for the IoC container to create and inject the resources it needs.

IoC embodies one of the principles of object-oriented design - the Hollywood law: "don't find us, we find you"; that is, the IoC container helps the object find the corresponding dependent object and inject it, rather than the object actively finding it.

1.3、IoC和DI

DI - Dependency Injection, that is, "dependency injection": the dependency between components is determined by the container at runtime. To put it 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 only need to specify the resources required by the target through simple configuration without any code, and complete our own business logic, without caring about where the specific resources come from and who implements them.

The key to understanding DI is: "who depends on whom, why does it need to depend, who injects who, and what is injected", then let's analyze it in depth:

●Who depends on whom: Of course, the application depends on the IoC container;

●Why do you need dependencies: The application needs the IoC container to provide the external resources required by the object;

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

● What to inject: It is to inject external resources (including objects, resources, constant data) required by an object.

What is the relationship between IoC and DI? In fact, they are descriptions of the same concept from different perspectives. Because the concept of inversion of control is rather vague (it may only be understood as the level of container control objects, it is difficult for people to think of who will maintain the object relationship), so the 2004 master Martin Fowler A new name has been given: "dependency injection". Relative to IoC, "dependency injection" clearly describes "the injected object depends on the IoC container to configure the dependent object".

I have read a lot of articles on the understanding of Spring's Ioc, and many people's explanations of Ioc and DI are obscure and difficult to understand. The technical expert wrote very easy to understand. He clearly explained every word in IoC (Inversion of Control) and DI (Dependency Injection), and after reading it, it gave people a feeling of enlightenment. I believe that the understanding of Ioc for those who are new to the Spring framework should be of great help.

2. Share the easy-to-understand explanation of IoC and DI on Bromon's blog
2.1, IoC (Inversion of Control)
First of all, I want to talk about IoC (Inversion of Control, Inversion of Control). This is the core of spring, throughout. The so-called IoC, for the spring framework, means that spring is responsible for controlling the life cycle of objects and the relationship between objects. What does this mean, to give a simple example, how do we find girlfriends? A common situation is that we go everywhere to see where there are mm who are beautiful and have a good figure, and then inquire about their hobbies, QQ numbers, phone numbers, ip numbers, iq numbers... , and try to get to know them and vote for them. Give what you want, and then hehe... This process is complicated and profound, and we must design and face each link by ourselves. The same is true for traditional program development. In an object, if you want to use another object, you must get it (new one yourself, or query one from JNDI), and destroy the object after use (such as Connection, etc.), Objects are always coupled to other interfaces or classes.

So how does IoC do it? A bit like finding a girlfriend through a matchmaking agency, a third party was introduced between me and my girlfriend: a marriage agency. The matchmaking agency manages a lot of profiles of men and women. I can submit a list to the matchmaking agency and tell it what kind of girlfriend I want to find, such as looking like Li Jiaxin, body like Lin Xilei, singing like Jay Chou, speed like Carlos, technology like Qi Dane and the like, and then the matchmaking agency will provide a mm according to our requirements, we just need to go to her to fall in love and get married. Plain and simple, we throw an exception if the matchmaking agency offers us an unqualified candidate. The whole process is no longer controlled by myself, but by a container-like agency like matchmaking. This is the development method advocated by Spring. All classes will be registered in the spring container, telling spring what you are and what you need, and then spring will take the initiative to give you what you want when the system is running at an appropriate time. , while also handing you over to other things that need you. The creation and destruction of all classes are controlled by spring, that is to say, the object that controls the life cycle of an object is no longer the object that refers to it, 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 of the key points of IoC is to dynamically provide an object with other objects it needs while the system is running. This is achieved through DI (Dependency Injection). For example, object A needs to operate the database. In the past, we always had to write code in A to obtain a Connection object. With spring, we only need to tell spring that a Connection is needed in A. As for how to construct this Connection, when to construct it, A does not need to know. When the system is running, spring will create a Connection at the appropriate time, and then inject it into A like an injection, thus completing the control of the relationship between various objects. A needs to rely on Connection to run normally, and this Connection is injected into A by spring, so the name of dependency injection comes from. So how is DI implemented? An important feature after Java 1.3 is reflection, which allows programs to dynamically generate objects, execute methods of objects, and change properties of objects at runtime. Spring implements injection through reflection.

After understanding the concept of IoC and DI, everything will become simple and clear, and the rest of the work is just stacking wood in the framework of spring.

3. My understanding of IoC (Inversion of Control) and DI (Dependency Injection)

In the usual java application development, we need at least two or more objects to cooperate to complete a certain function or to complete a certain business logic. When Spring is not used, each object needs to use his When cooperating, you must use syntax like new object() to create the cooperating object. This cooperating object is created by yourself. The initiative to create the cooperating object is in your own hands. Which cooperating object do you need? , just take the initiative to create, the initiative and creation timing of creating cooperative objects are controlled by yourself, and this will make the coupling between objects high, object A needs to use cooperative object B to complete one thing together, A If you want to use B, then A has a dependency on B, that is, there is a coupling relationship between A and B, and they are tightly coupled, but after using Spring, it is different. The job of creating a cooperative object B is It is done by Spring. Spring creates the B object and stores it in a container. When the A object needs to use the B object, Spring takes the B object that A wants to use from the container where the object is stored, and then gives it to A. Object usage, as for how Spring creates that object, and when it creates the object, object A does not need to care about these details (I don't care when you were born and how you were born, just help me with my work) , After A gets the object that Spring gives us, the two people can work together to complete the work to be done.

Therefore, IoC (Inversion of Control) means that the control of the created object is transferred. In the past, the initiative and creation timing of the object was controlled by itself, but now this power is transferred to a third party, such as transferring to With the IoC container, it is a factory specially used to create objects. It will give you whatever object you want. With the IoC container, the dependencies will change, and the original dependencies will be gone. They all depend on IoC. Containers, through the IoC container to establish the relationship between them.

This is my understanding of Spring's IoC (Inversion of Control). DI (dependency injection) is actually another term for IOC. DI was first proposed by Martin Fowler in a paper in early 2004. He concluded: What was the control reversed? That is: the way to obtain dependent objects is reversed.

4. Summary

For the core concept of Spring Ioc, I believe that everyone who learns Spring will have their own understanding. There is no absolute standard answer to this conceptual understanding. The benevolent see benevolence and the wise see wisdom. If there is any misunderstanding or misunderstanding, please feel free to correct me!

Guess you like

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