coding ++: Spring_IOC (Inversion of Control) Detailed

What IoC is:

  1): Ioc-Inversion of Control, that is, "inversion of control", not a technical, but a design idea.

  2): in Java development, Ioc means that you designed objects to the container control, rather than the traditional direct control over your internal object.

Ioc understand how good it?

Ioc good key to understanding is to define "who is who controls what control, why is reversed (reversal should have a positive turn), which reversed the terms," ​​that we have to analyze in depth:

● Who controls who and what control: traditional Java SE programming, we directly create objects through new inside the object, a program initiative to create dependent objects; and IoC is a special container to create these objects, which come from the Ioc container control to create an object;

  Who controls? Of course, the control object is IoC container;

  What control? That is the main control external access to resources (including not just objects such as files, etc.).

● Why is reversed, what reversed: there have reverse forward, legacy applications by our own initiative in the object control to directly dependent objects, that is, forward;

● reversed by the container is to help create and inject dependent objects;

● Why is reversed? Because the container to help us find and inject the dependent objects, the object of accepting it passively dependent objects, it is the reverse;

● What is reversed? The acquisition is dependent on the object reversed.

What IoC can do:

IoC is not a technology, but an idea, an important rule of object-oriented programming that guides how we design a loosely coupled, better program. Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible.

1): Actually, IoC programming bring the biggest change is not from the code, but in terms of ideology, change "master-slave position change" has occurred.

2): The application was originally boss, to get what resources are to take the initiative, but in IoC / DI thought, the application becomes passive  

        A passive waiting IoC container to create and inject the resources it needs.

3): IoC good indication of one of object-oriented design principles. Hollywood rule: "Do not come to us, we are looking for you"; that is, to help find the appropriate objects by the IoC container and injection of dependent objects, rather than take the initiative to find the object.

IoC 和 IN:

DI-Dependency Injection, namely "dependency injection": dependency relationships between components at runtime is determined by the vessel, said the image that will be injected by the container dynamic dependencies into a component.

Dependency injection is not intended to bring more functionality to software systems, but to enhance component reuse frequencies, and for the system to build a flexible, scalable platform.

Via dependency injection mechanism, we need only a simple configuration, without any code can specify the target resources needed to complete their business logic without having to be concerned about the specific resources come from, who to achieve.

DI is the key to understanding: "Who dependence, why rely on, who is who inject, inject something" that we have to analyze in depth:

● Who depends on whom: of course, is application dependent on the IoC container;

● Why rely on: the application needs IoC container to provide external resources required objects;

● Who injection: it is clear that the object is injected into the IoC container application to an object, the application-dependent;

● injected What: an object that is injected into the external resources required (including objects, resources, constant data).

IoC and DI by what to do with it?

  In fact, they are the same concept of different angles description, due to the inversion of control concepts vague (probably only be understood as a container control object this level, it is hard to think of going to protect object-relational),

  So gurus Martin Fowler in 2004 and given a new name: "Dependency Injection", relatively speaking IoC, "dependency injection" clearly describes the "object dependency is injected IoC container configuration dependent objects."

 

 

Read a lot of articles on Spring's Ioc understand, a lot of people Ioc interpretation and DI are obscure, anyway, is a kind of inexplicable feeling, after reading it is still a loss, the feeling is open Tao the cattle were written especially user-friendly technology,

He clearly explains IoC (Inversion of Control) and DI (dependency injection) in every word, giving a feeling of clear insight after reading it. I believe that for beginners who Ioc understanding of Spring Framework should be of great help.

Guess you like

Origin www.cnblogs.com/codingmode/p/12593439.html