In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is IOC and DI". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What is IOC and DI"!
1. What IOC is IOC - Inversion of Control, that is,"inversion of control", is not a technology, but a design idea. In Java development, Ioc means giving control of your designed objects to containers, rather than the traditional direct control within your objects. How to understand Ioc? The key to understanding Ioc is to be clear about "who controls who, what, and why it is reversed (If there is a reversal, there should be a positive turn), which aspects are reversed ", then let's analyze in depth: who controls who and what: traditional Java SE programming, we directly create objects through new inside the object, 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 instead of explicitly using new; who controls who? Of course it is the IOC container that controls the object; controls what? That is, it mainly controls the acquisition and life cycle of external resources (not only objects but also files). Why is it reversed, which aspects are reversed: there is a reversal, there is a positive rotation, traditional applications are actively controlled by ourselves in the object to directly obtain dependent objects, that is, positive rotation; and inversion is by the container to help create and inject dependent objects; why is it reversed? Because the container helps us find and inject dependent objects, objects are only passively accepting dependent objects, so it is inverted; what aspects are inverted? The acquisition of dependent objects is reversed.
To illustrate, traditional programming, as shown in Figure 1, actively creates related objects and then combines them:
Figure 1: Traditional application structure diagram
When IOC containers are available, they are no longer actively created in the client class, and the program structure diagram becomes as shown in Figure 2:
IOC is not a technology, but an idea, an important object-oriented programming principle that can guide us in designing loosely-coupled, better-performing programs. In traditional applications, we create dependent objects inside 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, and the container injects composite objects, so the objects are loosely coupled, which is also convenient for testing, conducive to functional reuse, and more importantly, makes the entire architecture of the program very flexible.
In fact, the biggest change brought by IOC to programming is not from the code, but from the thought, there has been a change of "master-slave transposition." The application is the boss, what resources to obtain are proactive, but in IOC/DI thinking, the application becomes passive, passive waiting for the IOC container to create and inject the resources it needs. IOC is a good example of one of the object-oriented design principles-Hollywood Law: "Don't look for us, we look for you"; that is, IOC containers help objects find corresponding dependent objects and inject them, rather than objects actively looking for them. 3. IOC and DIDI-Dependency Injection, i.e."Dependency Injection": The dependency relationship between components is determined by the container at runtime, and the image is said that a dependency relationship is dynamically injected into the component by the container. The purpose of dependency injection is not to bring more functionality to the software system, but to increase the frequency of component reuse and build a flexible and extensible platform for the system. Through dependency injection, we can specify the resources needed by the target through simple configuration without any code to 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 needs to depend on, who injects who, what is injected", then let's analyze in depth: who depends on whom: of course, the application depends on the IOC container;
Why dependencies are needed: Applications need IOC containers to provide external resources needed by objects;
Who injects who: Obviously the IOC container injects an object into the application, an object that the application depends on;
What is injected: 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. Because the concept of inversion of control is vague (it may only be understood as the level of container control object, and it is difficult to think of who will maintain the object relationship), Martin Fowler, a master figure in 2004, gave a new name: "dependency injection." Compared with IOC,"dependency injection" clearly describes "injected object depends on IOC container configuration dependent object." I have read many articles on Ioc understanding of Spring. Many people's explanations of Ioc and DI are obscure. Anyway, it is a kind of unclear feeling. After reading it, I am still confused. After reading this technical cow's blog, there is a kind of open-minded research. He clearly explains every word in IOC(inversion of control) and DI(dependency injection). After reading it, it gives people a feeling of open-minded. I believe that for beginners of the Spring framework understanding of IOC should be very helpful. 4. Meaning of IOC and DI In normal Java application development, we need at least two or more objects to cooperate to implement a certain function or complete a certain business logic. When Spring is not used, each object needs to use its cooperative object or dependent object. Syntax like new object() is used to create the cooperative object. This cooperative object is created by itself. The initiative to create cooperative objects is in their own hands, which cooperative objects they need, they take the initiative to create, the initiative to create cooperative objects and the creation timing are controlled by themselves, and this will make the coupling degree between objects high. Object A needs to use cooperative object B to accomplish one thing together, and A needs to use B, then A has a dependence on B, that is, there is a coupling relationship between A and B, and they are closely coupled together. After using Spring, it is different. The work of creating the cooperative object B is done by Spring. Spring creates the B object and then 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 the A object. As for how Spring creates the object and when it creates the object, the A object does not need to care about these details.(I don't care when you were born, how you were born, just help me work), after A gets the object B given to us by Spring, the two people can work together to complete the work to be completed. So control inverts IOC (Inversion of Control) means that the control right to create objects is transferred. In the past, the initiative and timing of creating objects were controlled by themselves, but now this power is transferred to a third party, such as the transfer to the IOC container, which is a factory dedicated to creating objects. It gives you what objects you want. With the IOC container, the dependencies change, and the original dependencies disappear. They all depend on the IOC container. The relationship between them is established through the IOC container. DI(dependency injection) is actually another term for IOC, and DI was first proposed by Martin Fowler in a paper in early 2004. He concluded: what is controlled is reversed? It's the reverse of how you get dependent objects.
At this point, I believe everyone has a deeper understanding of "what is IOC and DI," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.