Spring Ioc/di's understanding

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

These days to re-learn spring, on the Internet to find the relevant PPT, when you see the spring IOC chapter, the first roughly browse the content, there are nearly 50 pages of content, the heart of the ~qaq~, read these content can have a deeper understanding of the IOC. But the egg, took two days to finally finish reading, but also wrote the sample code, but the IOC's understanding or a face, then think of the degree Niang, really degree niang or very strong, find an article about the spring IOC understanding of articles, read this article after the enlightened, such as Mu Spring breeze. Let's take a look at this article: http://www.cnblogs.com/xdp-gacl/p/4249939.html

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 1-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 1-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 ;

What is injected: the external resources (including objects, resources, constant data) that are required 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 (perhaps 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 gave a new name: "Dependency Injection", relative to 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 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 proper time, 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.

Third, summary

The above understanding of the spring IOC, I personally more inclined to the second, because the second more tease, easy to understand! Let's talk about my understanding of the spring IOC: with spring IOC, we're not going to create one or more of the object instances we need, like traditional Java programming, and all of the dependent object instances are given to the IOC container to create and manage, Duzi. The dependent objects are then injected into the current object. Because the creation and management of objects are given to the spring IOC container, it is no longer necessary to go to new in the same way as traditional programming, thus reducing the coupling.

Spring Ioc/di's understanding

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.