[Posts] What is IOC (inversion control), DI (dependency injection)

What is IOC (inversion control), DI (dependency injection)

Original Address (summary part of the contents): https://blog.csdn.net/qq_22654611/article/details/52606960/

Spring Framework learned some people will have heard of the Spring IoC (Inversion of Control), DI (dependency injection) of these two concepts for the beginner Spring people who always feel IoC, DI these two concepts is unclear It is difficult to understand, and share online today some of the technical understanding of the large cattle IOC Spring framework as well as talk about my understanding of the Spring Ioc.

A share Iteye opening Tao wonderful explanation of the Ioc

  First, I want to share is Iteye Tao opened the technical understanding of the cattle of the IOC Spring framework, written in a very easy to understand, following all from the original, the original address: http: //jinnianshilongnian.iteye.com/blog/1413846

1.1 What is the IoC

  Ioc-Inversion of Control, that is, "inversion of control", not a technical, but a design idea. In Java development, Ioc means that you designed objects to the container control, rather than the traditional direct control over your internal object. Ioc understand how good it? Ioc good key to understanding is to define "who is who controls what control, why is reversed (reversal should have a positive turn), which reversed the terms," ​​that we have to analyze in depth:

  ● Who controls who and what control: traditional Java SE programming, we directly create objects through new inside the object, a program initiative to create dependent objects; and IoC is a special container to create these objects, which come from the Ioc container create a control object; who controls? Of course IoC container control object; what control? That is the main control external access to resources (including not just objects such as files, etc.).

  ● Why is reversed, what reversed: there have reverse forward, legacy applications by our own initiative in an object to acquire control directly dependent objects, that is, forward; while the reverse is from the container to help create and inject dependent objects; why is reversed? Because the container to help us find and inject the dependent objects, the object of accepting it passively dependent objects, it is the reverse; what reversed? The acquisition is dependent on the object reversed.

  With a legend to explain that traditional programming shown in Figure 2-1, the initiative to create the relevant objects are then combined:

1-1 a schematic view of FIG legacy applications

  When there IoC / DI container, no longer active in the client to create a class of objects, shown in Figure 2-2:

Figure 1-2 a schematic diagram of the program structure IoC / DI container

1.2 What do IoC

  IoC is not a technology, but an idea, an important rule of object-oriented programming that guides how we design a loosely coupled, better program. Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible.

  In fact, IoC programming bring the biggest change is not from the code, but ideologically, "master-slave transposition" of change. The application was originally boss, to get what resources are to take the initiative, but in IoC / DI thought, the application becomes passive, and passive waiting IoC container to create and inject the resources it needs.

  IoC good indication of one of the objects oriented design rules - Hollywood rule: "Do not come to us, we are looking for you"; that is, to help find the appropriate objects by the IoC container object and dependency injection, rather than take the initiative to find the object.

1.3, IoC 和 OF

  DI-Dependency Injection, namely "dependency injection": dependency relationships between components at runtime is determined by the vessel, said the image that will be injected by the container dynamic dependencies into a component. Dependency injection is not intended to bring more functionality to software systems, but to enhance component reuse frequencies, and for the system to build a flexible, scalable platform. Via dependency injection mechanism, we need only a simple configuration, without any code can specify the target resources needed to complete their business logic without having to be concerned about the specific resources come from, who to achieve.

  DI is the key to understanding: "Who dependence, why rely on, who is who inject, inject something" that we have to analyze in depth:

  ● Who depends on whom: of course, is application dependent on the IoC container;

  ● Why rely on: the application needs IoC container to provide external resources required objects;

  ● Who injection: it is clear that the object is injected into the IoC container application to an object, the application-dependent;

  ● injected What: an object that is injected into the external resources required (including objects, resources, constant data).

  IoC and DI by what to do with it? In fact, they are the same concept describing a different point of view, due to the inversion of control concepts vague (probably only be understood as a container control object this level, it is hard to think of going to protect object-relational), so in 2004 the master at Martin Fowler has given a new name: "Dependency injection", relatively speaking IoC, "dependency injection" clearly describes the "object dependency is injected IoC container configuration dependent objects."

  Read a lot of articles on Spring's Ioc understand, a lot of people Ioc interpretation and DI are obscure, anyway, is a kind of inexplicable feeling, after reading it is still a loss, the feeling is open Tao the cattle were written especially user-friendly technology, he clearly explains IoC (inversion of control) and DI (dependency injection) in every word, giving a feeling of clear insight after reading it. I believe that for beginners who Ioc understanding of Spring Framework should be of great help.

Second, the share on the blog Bromon easy to understand explanation of the DI and IoC

2.1, IoC (Inversion of Control)

  First of all want to say that IoC (Inversion of Control, Inversion of Control). This is the core of spring, throughout. The so-called IoC, for spring framework, it is the relationship between the spring be responsible for controlling the lifetime of the object and the object. What does it mean, give a simple example of how we are to find a girlfriend? Common situation is that we see everywhere where there was pretty tall and good mm, then inquire about their hobbies, qq number, phone number, ip number, iq No. ........., think of ways to know them, they cast good to send it to, then hey ...... this process is complex esoteric, we must confront their own design and every aspect. The traditional application development, too, in an object, if you want to use another object, you have to get it (himself a new, or a query from JNDI), even after you are finished using the object is destroyed (such as Connection, etc.), and other objects will always interface or class coupled together.

  So IoC is how to do it? A bit like looking for a girlfriend through matchmaking between my girlfriend and introduces a third party: marriage agency. Matchmaking management information on a lot of men and women, I can make a list of the matchmaking, tell it what I want to find a girlfriend, for example, looks like Michelle Reis, built like Lin Xi Lei, sing like Jay, like Carlos speed, techniques like Qi Dardenne and the like, and matchmaking will be in accordance with our requirements, providing a mm, we just go to her and fall in love, get married on the line. Simple and clear, if we give matchmaking candidates do not meet the requirements, we will throw an exception. The whole process is no longer controlled by my own, but there is such a matchmaking agency similar container to control. Spring encourages development approach is the case, all classes will be registered in the spring container, tell spring you are something, you need something, then spring will run the appropriate time in the system, the things you want to take the initiative to give you as well as other needs you to put your stuff. All of the class is created, destroyed by the spring to control, that control object is no longer referenced its object life cycle, but the spring. For a specific subject, before it is to control other objects, all objects are now controlled spring, so called inversion of control.

2.2, DI (dependency injection)

  IoC is a focus of the system is running, the dynamic of other objects you need to provide to an object. This is achieved by DI (Dependency Injection, dependency injection). A need to manipulate database objects such as, in the past, we always want to write their own code A, to get a Connection object, with the spring we just need to tell spring, A requires a Connection, Connection as to how this structure, when constructed, A need to know. The system is running, spring will create a Connection at the appropriate time, and then the same as injections, which is injected into A, thus completing the control of the relationships between the various objects. A need to rely on Connection to run properly, and this Connection is injected into A by the spring, and dependency injection name it came. So DI is how to achieve it? An important feature of Java is 1.3 after reflection (Reflection), when the program is running that allows the generation of dynamic objects, the object execution method, changing object properties, spring implantation is achieved by reflection.

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

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11880860.html