Talking about the understanding of spring IOC

Source: Internet
Author: User
Tags ip number reflection java se

Https://www.cnblogs.com/xdp-gacl/p/4249939.html

Aloof, pale wolf

Only to find a way to succeed, not to find excuses for failure!

Talking about the understanding of spring IOC

People who have studied the spring framework will surely hear the two concepts of Spring's IOC (inversion of Control) and DI (Dependency injection), which, for beginners of spring, always feel that the two concepts of IOC and DI are ambiguous and difficult to understand. Today, we share some of the technologies on the Web. Daniel's understanding of the spring Framework's IOC and my understanding of the spring IOC.

I. Sharing the iteye of the IOC

The first thing to share is the iteye, the technical bull of the spring framework of the IOC understanding, written very easy to understand, the following are all from the original, the original address: http://jinnianshilongnian.iteye.com/blog/1413846

1.1. What is the IOC?

  ioc-inversion of control, or "inversion of controls", is not a technique, 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 make it clear "who controls who, what controls, why it is reversed (there should be a reversal in reverse), and which aspects are reversed", so let's look at it in depth:

who controls who, what controls: traditional Java SE Programming, we create objects directly within objects through new, is the program to create dependent objects, and the 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.

Illustrated by the legend, the traditional programming 2-1, are actively to create related objects and then combined together:

Figure 1-1 Legacy applications

When you have a Ioc/di container, you are no longer actively creating these objects in the client class, as shown in 2-2:

Figure 1-2 Ioc/di container Post-Program structure

1.2. What the IOC can do

IoC is not a technology, but an idea, an important object-oriented programming law that can guide us in designing 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": The 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," Let's look at it in depth:

Who depends on who: of course the application relies on the IOC container ;

Why you need to rely on: applications need an IOC container to provide the external resources required by the object ;

who injects who: it is obvious that an IOC container injects an object into an application, an application-dependent object ;

< Strong style= "FONT-SIZE:14PX; Line-height:1.5em; padding:0px; margin:0px; " What is injected: is inject an object with the external resources (including objects, resources, constant data) .

IOC and Di by what relationship ? In fact they is a different angle description of the same concept , because the concept of inversion of control is ambiguous (perhaps just as a layer of container control objects, It's hard to think of anyone to maintain the object relationship, so the 2004 master figure Martin Fowler again gave a new name: "Dependency Injection", relative to IOC, " Dependency injection" Strong style= "FONT-SIZE:14PX; Line-height:1.5em; padding:0px; margin:0px; " > clearly describes "the injected object relies on the IOC < Span style= "color: #ff0000;" > container configuration dependent objects ".

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 reversal)

The first thing to say about IoC (inversion of control, inversion of controls). 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 go everywhere to 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 spring will give you what you want when the system runs to the right time. , but it also gives you the other things you need. All classes are created and destroyed 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)

  A key point of the IOC is to dynamically provide the other objects it needs to an object during the system's operation. 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 this connection how to construct, when to construct , 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 into a by spring, and the name of the dependency injection comes in. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows the program to dynamically generate objects, execute methods of objects, and change the properties of objects when it is run, 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 the normal Java application development, we want to implement a certain 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, they have to use like new object () Such a syntax to create a cooperative object, this cooperation object is created by their own initiative, the initiative to create a cooperative object in their own hands, which partners need to work on their own initiative to create, the initiative to create a cooperative object and the creation of the timing is self-control, This will make the coupling between objects high, a object needs to use partner B to accomplish one thing, a to use B, then A has a dependency on B, that is, A and b there is a coupling relationship, and is tightly coupled, and the use of spring is not the same, The work of creating partner B is done by spring, Spring creates a B object, then stores it in a container, and when the A object needs to use a B object, spring takes the B object that is to be used from the container that holds the object, and then gives it to the A object, As to how spring creates that object, and when to create a good object, the A object does not need to be concerned with these details (when you were born, how you were born I don't 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 of the previously created object and the creation time is controlled by itself, and now this power is transferred to the third party , For example, the transfer to the IOC container, it 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: the way to get dependent objects 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!

Talking about the understanding of 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.