Understanding of Spring IOC

Source: Internet
Author: User

        people who have studied the spring framework must have heard the two concepts of Spring's IOC (inversion of Control) and Di (Dependency injection), and for those who are beginning to spring, the idea that the IOC and DI concepts are ambiguous, is very difficult to understand, today and you share some of the technology on the internet Daniel's understanding of the spring Framework's IOC and about my understanding of the spring IOC. I. Sharing Iteye's wonderful interpretation of the IOC 1.1, what is the IOC ioc-inversion of control, that is, "controlled inversion", not what technology, but a design idea. In Java development, IOC means handing over your designed objects to container control rather than the traditional direct control within your object. How to understand the good IOC? The key to understanding the IOC is to be clear about "who controls who, what controls, why it is reversed (there should be a reversal in reverse), and what is reversed", so let's go into a deep analysis: who controls who, what controls: Traditional Java SE Programming, we create objects directly within objects through new, Is that the program is actively creating dependent objects, and IOC has a special container to create these objects, that is, the IOC container to control the creation of objects; who controls who? The IOC container, of course, controls the object;  That is the primary control of external resource acquisition (not just objects such as files, etc.). Why reverse, what is reversed: there is a reversal of the forward, the traditional application is our own active control in the object to directly obtain the dependent object, that is, the reverse is the container to help create and inject dependent objects; Because the container helps us to find and inject dependent objects, the object is only passively accepting dependent objects, so it is reversed;  The acquisition of the dependent object is reversed. 1.2. What IOC can do IOC is not a technology, but an idea, an important principle of object-oriented programming, which can guide us to design loosely coupled, better programs. Traditional applications are created by us to create dependent objects within the class, which causes the class to be highly coupled to the class and difficult to test; with the IOC container, the control of creating and finding dependent objects is given to the container, which is injected by the container, so that the object is loosely coupled with the object, so it is also easy to test.  Facilitates functional reuse and, more importantly, makes the entire architecture of the program very flexible. In fact, the biggest change of the IOC to programming is not from the code, but from the thought, the "master-slave Transposition" change. The application was originally the boss, to get what resources are active attack, but in Ioc/di thought, the application becomes passive, passively wait for the IOC container to create and inject the resources it needs。 IOC is a good embodiment of the object-oriented design of one of the rules of Hollywood: "Do not call us, we find you", that is, the IOC container for the object to find the corresponding dependent objects and injected, rather than the object to be actively looking for. 1.3. IOC and DI di-dependency injection, or "dependency injection": dependencies between components are determined by the container at run time, in the image, that is, the container dynamically injects a dependency into the component. The purpose of dependency injection is not to bring more functionality to a software system, but to increase the frequency of component reuse and to build a flexible and extensible platform for the system.  Through the dependency injection mechanism, we only need to use simple configuration, without any code to specify the resources required by the target, to complete its own business logic, without the need to care about where the specific resources come from and by whom.  The key to Understanding di is: "Who depends on who, why they need it, who injects it, and what is injected into it," then let's analyze it: who depends on who: Of course, the application relies on the IOC container; The application needs an IOC container to provide the external resources required by the object;  Who injects: it is obvious that an IOC container injects an object into an application, an object that the application relies on, and what it injects: the external resources (including objects, resources, constant data) that are needed to inject an object. What is the relationship between the IOC and di? In fact, they are different angles of the same concept, because the concept of inversion of control is ambiguous (may be just understood as a container control object this level, it is difficult to think of who to maintain the object relationship), so the 2004 master figure Martin Fowler again gave a new name: "Dependency Injection",  In contrast to the IOC, "dependency Injection" clearly describes the "injected object relies on the IOC container configuration dependent object". Read many of the spring's IOC understanding of the article, a lot of people on the IOC and DI interpretation are obscure, anyway, is a kind of inexplicable, the sense of the unknown, after reading is still confused, the feeling is open Tao This technical ox people write particularly easy to understand, he clearly explained the IOC (control inversion) and Di ( Dependency injection) in each of the words, after reading to give people a sense of the enlightened. I believe that it is very helpful for the people of the spring framework to understand the IOC. Ii. sharing Bromon's blog on the IOC and DI Easy to understand 2.1, IOC (control inversion) first of all want to talk about the IOC (inversion of control, inversion). This is the core of spring, throughout. The so-called IOC, for the spring framework, is the responsibility of spring to control the object's life cycle and the relationship between objects. What does that mean, for example, how do we find a girlfriend? The common situation is that we are everywhereTo see where there is a beautiful body and good mm, and then inquire about their interests, QQ number, telephone number, IP number, IQ number ..., find ways to know them, give them what they want, then hey ... The process is complex and profound, and we have to design and face each link ourselves.  The same is true of traditional program development, in an object, if you want to use another object, you have to get it (your own new one, or a query from Jndi), after the use of the object will be destroyed (such as connection, etc.), the object will always and other interfaces or classes together. So how does the IOC do it? It's kind of like finding a girlfriend through a dating agency, introducing a third party between me and my girlfriend: the Marriage Institute. Matchmaking management of a lot of men and women's information, I can give a list of matchmaking, tell it I want to find a girlfriend, such as like Michelle Reis, figure like Lin Xire, singing like Jay Chou, speed like Carlos, technology like Zidane, and then the matchmaking will be according to our requirements, provide a mm, We just have to go to love her and get married. As simple as it is, if a matchmaking person doesn't meet our requirements, we'll throw an exception. The whole process is no longer controlled by myself, but by a similar container-like institution that has a matchmaking system. This is how spring advocates for development, and all classes are registered in the spring container, telling spring what you are, what you need, and then spring will give you what you want when the system runs to the right time, as well as handing you over to other things that need you. The creation and destruction of all classes are controlled by  spring, which means that the object that controls the lifetime of the object is no longer a reference to it, but spring. For a specific object, it was previously controlled by other objects, and now all objects are controlled by spring, so this is called control inversion. 2.2. DI (Dependency injection) One of the main points of the IOC is to dynamically provide the other objects it needs to an object while the system is running. This is achieved through DI (Dependency injection, Dependency injection). For example, object A needs to manipulate the database, before we always have to write code in a to get a connection object, with  spring we just need to tell spring,a need a connection, As for how this connection is constructed, when it is constructed, a does not need to know. When the system is running, Spring creates a connection at the right time, and then, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on  connection to function properly, and this connection is injected by spring into a, dependent injectionThe name just came. 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 the properties of objects at runtime, and spring is injected through reflection. With the understanding of the IOC and Di concepts, everything becomes straightforward and the rest of the work is just piling up wood in the spring frame. Iii. My understanding of IOC (inversion of Control) and Di (dependency injection) in peacetime Java application development, we want to implement a function or to complete a business logic at least two or more objects to collaborate to complete, when not using spring, Each object in need to use his partner object, he will use a syntax like new object () to create a cooperative object, the partner is created by their own initiative, the creation of the initiative of the cooperation object in their own hands, the need for which partners, the initiative to create, The initiative to create a partner and the timing is self-control, and this will make the coupling between the object is high, a object needs to use partner B to accomplish one thing, a to use B, then A has a dependence on B, that is, A and b there is a coupling relationship, and is tightly coupled together, Instead of using spring, the work of creating partner B is done by spring, Spring creates a B object, and then stores it in a container, and when the A object needs to use a B object, spring takes the B object from the container that holds the object, Then to the a object to use, as to how spring creates that object, and when to create a good object, a object does not need to care about these details (when you were born, how it was born I do not care, can help me work on the line), a get spring to our object,  Two people together to complete the work to complete. So the control reversal IOC (inversion of control) is to say that the creation of the object is transferred, the initiative and creation time of the previously created object is controlled by itself, and now this power is transferred to a third party, such as transferred to the IOC container, which is a factory dedicated to creating objects  , you want what object, it gives you what object, with the IOC container, the dependency is changed, the original dependency is gone, they all rely on the IOC container, through the IOC container to establish their relationship. This is my understanding of Spring's IOC (inversion of control). Di (Dependency injection) is actually another argument of the IOC, which was first proposed by Martin Fowler in a paper in early 2004. He concludes: What control is reversed? is: Access toThe way the object is reversed. Iv. Summary for the core concept of spring IOC, I believe everyone who learns spring will have their own understanding. This conceptual understanding does not have the absolute standard answer, the benevolent see of the beholder. If there is understanding is not in place or understand the wrong place, welcome the vast number of garden friends to correct me!   who depends on who: Of course, the application relies on the IOC container, why it needs to be relied on: The application needs an IOC container to provide the external resources that the object needs; who injects it: obviously an IOC container injects an object into an application, an application-dependent object Inject something: the external resources (including objects, resources, constant data) that are needed to inject an object.

Understanding of the Spring IOC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.