Understanding dependency injection and control inversion in spring

Anyone who has studied the spring framework must have heard of the two concepts of IOC (inversion of control) and di (dependency injection) of spring. For beginners of spring, they always feel that the two concepts of IOC and di are vague and difficult to understand. Today, I share with you some online technology Bulls' understanding of the IOC of the spring framework and my understanding of spring IOC.

What is IOC

IOC - inversion of control, or "control inversion", is not a technology, but a design idea. In java development, IOC means that the object you designed is controlled by the container, rather than the traditional direct control inside your object.

IOC, which is the core of spring, runs through the whole process. The so-called IOC, for the spring framework, is that spring is responsible for controlling the life cycle of objects and the relationship between objects. What does this mean? For a simple example, how do we find girlfriends? The common situation is that we go everywhere to see where there are beautiful and good-looking mm, and then ask about their hobbies, QQ number, phone number, IP number, IQ number... To find a way to get to know them, give them what they want, and then hey... This process is complex and profound. We must design and face each link ourselves. The same is true for traditional program development. If you want to use another object in an object, you must get it (new one yourself, or query one from JNDI). After using it, you have to destroy the object (such as connection). The object will always be coupled with other interfaces or classes.

How to understand IOC well? The key to a good understanding of IOC is to clarify "who controls who, what, why reversal (if there is reversal, there should be positive rotation) and what aspects are reversed". Let's make an in-depth analysis:

● who controls who controls what: in traditional Java se programming, we directly create objects inside objects through new, and the program actively creates dependent objects; IOC has a special container to create these objects, that is, the IOC container controls the creation of objects; Who controls who? Of course, the IOC container controls the object; Control what? It mainly controls the acquisition of external resources (not only objects, including files, etc.).

● why is inversion and what aspects are reversed: if there is inversion, there is forward rotation. Traditional applications are actively controlled by ourselves in the object to directly obtain the dependent object, that is, forward rotation; The container helps create and inject dependent objects; Why reverse? Because the container helps us find and inject dependent objects, the objects only passively accept dependent objects, so it is inversion; What aspects are reversed? The acquisition of dependent objects is reversed.

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

Figure 1-1 schematic diagram of traditional application

When the IOC / di container is available, these objects are no longer actively created in the client class, as shown in Figure 1-2:

Figure 1-2 schematic diagram of program structure with IOC / di container

1.1. What can IOC do

IOC is not a technology, but an idea. It is an important rule of object-oriented programming. It can guide us how to design loosely coupled and better programs. In traditional applications, we actively create dependent objects within classes, resulting in high coupling between classes and difficult to test; With the IOC container, the control of creating and finding dependent objects is handed over to the container, and the container injects and combines objects. Therefore, objects are loosely coupled, which is also convenient for testing and function reuse. More importantly, the whole architecture of the program becomes very flexible.

In fact, the biggest change brought about by IOC to programming is not from the code, but from the idea of "master-slave transposition". The application is originally the boss. It takes the initiative to obtain any resources, but in the IOC / di idea, the application becomes passive and waits passively for the IOC container to create and inject the resources it needs.

IOC well embodies one of the object-oriented design principles - Hollywood Law: "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, rather than the object actively finds it.

1.1. 3ioc and di

Di - dependencyinjection, i.e. "dependency injection": the dependency between components is determined by the container at run time. Figuratively speaking, 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 improve 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 and complete our own business logic through simple configuration without any code, without caring where the specific resources come from and who implements them.

A key point of IOC is to dynamically provide an object with other objects it needs during system operation. This is through di (dependency injection). For example, object a needs to operate the database. In the past, we always wrote our own code in a to obtain a connection object. With spring, we just need to tell spring that a connection is needed in A. as for how and when to construct this connection, a does not 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, so as to complete the control of the relationship between various objects. A needs to rely on a connection to run normally, and this connection is injected into a by spring. That's the name of dependency injection. So how is di implemented? An important feature of Java 1.3 is reflection, which allows programs to dynamically generate objects, execute object methods and change object properties when running. Spring implements injection through reflection.

The key to understanding Di is: "who depends on whom, why it depends, who injects who, and what it injects". Let's make an in-depth analysis:

● who depends on whom: of course, the application depends on the IOC container;

● why dependency is needed: applications need IOC containers to provide external resources required by objects;

● who injects who: it is obvious that the IOC container injects an object of the application, which the application depends on;

● what is injected: it is to inject external resources (including objects, resources and constant data) required by an object.

What is the relationship between IOC and di? In fact, they are different descriptions of the same concept, Because the concept of control inversion is vague (it may only be understood as the level of container control object, which is difficult to think of who will maintain object relationship), in 2004, master Martin Fowler gave a new name: "dependency injection". Compared with IOC, "dependency injection" clearly describes that "the injected object depends on IOC container configuration dependency object".

summary

In fact, everyone has their own understanding of the concept of spring IOC. There is no standard answer. The general direction is right.

The above is all about understanding dependency injection and control inversion in spring. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>