Control inversion in spring IOC and dependency Injection di

Source: Internet
Author: User

Original:http://blog.163.com/[email protected]/blog/static/50639037200721345218382/

IoC (inversion of Control), which is the core of spring, runs through all the Times. 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, phone number, mobile phone number, MSN ..., find ways to know them, give it 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, where in an object, if you want to use a different object, you must create it (your own new one, or query one from Jndi), and then destroy the object (for example, connection, etc.) after use, and the object will always be combined with other interfaces or classes.

So how is the IOC doing it? It's kind of like finding a girlfriend through a dating agency. A third-party institution was introduced between me and my girlfriend: the marriage agency. Matchmaking management of a lot of men and women's information, I can make a list to the matchmaking, tell it I want to find a girlfriend, such as like Michelle Reis, figure like Lin Chi-ling, singing like Andy Lau, speed like Liu Xiang, tall like Yao Ming, 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. 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. If you do not understand, I decided to give up.

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.

Control inversion in spring IOC and dependency Injection di

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.