Spring Core (IOC control inversion)

Source: Internet
Author: User
Tags ip number



Ioc,inversion of control, which controls inversion, is managed by the container to manage dependencies between business objects, rather than by code in traditional ways.

Its essence, the control right from the application code to the external container, the transfer of control is so-called reversal, which brings the greatest benefit is to reduce the dependence between business objects, that is, to achieve understanding of decoupling.

Spring's IOC container is primarily implemented using DI (Dependency injection, Dependency injection). The lookup, positioning, and creation of objects are all managed by the container, and the container passes the object that conforms to the dependency through attributes (setters, etc.) or constructors to the desired object.

The following benefits can be created by using IOC:

1, query dependent operation and application code separation, a large number of reduced factory and Singleton, so that the code level clearer, the main reason is that we no longer find, locate, create and manage the dependencies between objects, are handed over to the IOC container management

2, no intrusion, do not need to rely on the container API, do not need to implement some special interfaces. This allows our controlled objects to be moved out of the container and used separately.

3. You can get an object directly from the IOC container and use it directly, regardless of the object creation process. (Factory mode)

Read an example from the Internet, feel good, post to help us understand:

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

In fact, we can think of the IOC as a distillation of the factory model, the IOC is like a big factory, but the objects generated in this large factory are defined in the XML file and then instantiated using Java's reflection. We did the same thing before spring, but Spring's IOC is more powerful and perfect.

There are some drawbacks to the IOC:

1, the use of reflection into the efficiency of some loss.

2, the lack of more intelligent IDE, when writing XML, error is not easy to expose, deferred until the run time to discover.

But this shortcoming is negligible relative to IOC's improved maintainability and flexibility.

At the end of the story, I suddenly thought of the "travel to America", the record company producer after Rebaca recorded the record, said to her: Don ' t call Us,we ' ll

It would be appropriate to use this phrase to describe the IOC.

Spring Core (IOC control inversion)

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.