Explanation of spring's Ioc inversion of control and DI dependency injection

Anyone who has studied the Spring framework must have heard of the two concepts of Spring 's IoC ( Inversion of Control ) and DI ( Dependency Injection ) . For those who are new to Spring , they always feel that the two concepts of IoC and DI are vague Yes, it is difficult to understand. Today, I will share with you some technical experts on the Internet's understanding of the IOC of the Spring framework and talk about my understanding of Spring Ioc .

1. Share Iteye ’s Kaitao ’s wonderful explanation of Ioc

The first thing I want   to share is Iteye 's Kaitao, a technical expert, who understands the IOC of the Spring framework . The writing is very easy to understand. The following content is all from the original text.

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 your designed objects to the control of the container, rather than the traditional direct control inside your objects. How to understand Ioc well ? The key to understanding Ioc well is to clarify " who controls whom, what is controlled, why is the reversal (if there is a reversal, there should be a forward rotation), and which aspects are reversed " , then let's analyze it in depth:

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

Why is it reversed, and what aspects are reversed : if there is a reverse, there is a positive rotation. In traditional applications, we actively control in the object to directly obtain the dependent objects, that is, the forward rotation; while the reverse is made by the container. To help create and inject dependent objects; why inversion? Because the container helps us find and inject dependent objects, the object only passively accepts the dependent object, so it is reversed; which aspects are reversed? The fetching of dependent objects is reversed.

  To illustrate with an illustration, the traditional program design is shown in Figure 2-1 , which is to actively create related objects and then combine them:

Figure 1-1 Schematic diagram of a 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 object-oriented programming rule, 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 given to the container, and the container Injecting combined objects, so objects are loosely coupled, which is also convenient for testing, conducive to function 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 ideological point of view, a " master-slave transposition " change has occurred . The application was originally the boss, and it took the initiative to obtain any resources, but in the IoC/DI thinking, the application became passive, passively waiting for the IoC container to create and inject the resources it needs.

IoC well embodies one of the object-oriented design rules - the Hollywood rule: " Don't look for us, we look for you " ; that is, the IoC container helps the object find the corresponding dependent object and inject it, instead of the object actively looking for it.

1.3 , IoC and DI

DI—DependencyInjection , that is, " Dependency Injection " : The dependency between components is determined by the container at runtime. In a visual sense, a certain dependency is dynamically injected into the component by the container . 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 scalable platform for the system. Through the dependency injection mechanism, we only need to specify the resources required by the target without any code through simple configuration, and complete our own business logic, without caring 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 whom, and what is injected " , so let's analyze it in depth:

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

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

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

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

What is the relationship between IoC and DI ? In fact, they are descriptions from different angles of the same concept . Since the concept of inversion of control is relatively 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 is given: " Dependency Injection " . Compared with IoC , " Dependency Injection " clearly describes " the injected object depends on the IoC container configuration dependent object " .

  I have read a lot of articles about Spring ’s Ioc understanding. Many people’s explanations for Ioc and DI are obscure. Anyway, it’s a feeling of being unclear and unclear. After reading it, I am still at a loss. It feels like Kaitao This technical genius is very easy to understand. He clearly explained every word in IoC ( Inversion of Control ) and DI ( Dependency Injection ) . After reading it, it gave people a feeling of enlightenment. I believe that the understanding of Ioc should be of great help to those who are new to the Spring framework.

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? The common situation is that we go around to see where there are girls with good looks and good body , and then inquire about their hobbies, qq numbers, phone numbers, ip numbers, iq numbers ... , try to get to know them, and do what they want Good to send what they want, and then hehe ... This process is complicated and profound, and we have to 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? It's a bit like finding a girlfriend through a matchmaking agency. A third party is introduced between me and my girlfriend: a marriage agency. The matchmaking agency has managed a lot of information about men and women. I can propose a list to the matchmaking agency and tell it what kind of girlfriend I want to find, for example, she looks like Li Jiaxin, has a body like Lin Xilei, sings like Jay Chou, speeds like Carlos, and has skills like Qi. Dane and the like, then the matchmaking agency will provide a mm according to our requirements , and we only need to fall in love with her and get married. Simple and clear, if the matchmaker gives us a candidate who does not meet the requirements, we will throw an exception. The whole process is no longer controlled by myself, but by a container-like organization like a matchmaking agency. This is the development method advocated by Spring . All classes will be registered in the spring container, tell spring what you are and what you need, and then spring will give you what you want when the system is running properly. , while also handing you over to others who need you. The creation and destruction of all classes are controlled by  spring , that is to say, it is no longer the object that refers to it, but spring that controls the life cycle of the object . 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 other objects it needs to an object during system operation. This is achieved through DI ( Dependency Injection , 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 and when to construct this Connection , A doesn't need to know. When the system is running, spring will create a Connection at an 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 springIn , the name of dependency injection comes from this. So how is DI implemented? An important feature after  Java 1.3 is reflection ( reflection ), which allows the program to dynamically generate objects, execute object methods, and change object attributes when the program is running. Spring implements injection through reflection.

  After understanding the concepts of IoC and DI , everything will become simple and clear, and the rest of the work is just to pile up blocks in the spring framework.

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 with an object, you have to use syntax like new object() to create the cooperating object. This cooperating object is created by yourself. The initiative to create a cooperating object is in your hands. Which cooperating object do you need? , take the initiative to create, the initiative and timing of creating cooperative objects are controlled by oneself, and this will make the coupling between objects high. Object A needs to use cooperative object B to complete one thing together. A 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 together, but it is different after using Spring . 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 out the B object that A wants to use from the container where the object is stored , and then gives it to the A object for use. As for how Spring creates the object and when the object is created, the A object does not need to care about these details ( I don’t care when you were born or how you were born, as long as you can help me with the work ) , after A gets the object given to us by Spring , the two of you can work together to complete the work to be done.

  So inversion of control IoC (Inversion of Control) refers to the transfer of the control right to create objects. In the past, the initiative and timing of creating objects were controlled by oneself, but now this power is transferred to a third party, such as the transfer to the IoC container, it is a factory specially used to create objects, what object you want, it will give you what object, with IoC container, the dependency will change, the original dependency is gone, they all depend on the IoC container Yes, the relationship between them is established through the IoC container.

  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 concludes: What is the control reversed? That is: the way to obtain dependent objects is reversed.

Four. 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 for this conceptual understanding, and the beholder has a different opinion. If there is something that is not well understood or misunderstood, please correct me!

 


Guess you like

Origin blog.csdn.net/zs520ct/article/details/79769083