Inversion of Control (IOC) and dependency injection (DI)

Inversion of Control (IOC) and Dependency Injection (DI)
IoC (Inversion of Control, IoC) Spring is a very important concept, it is not a technology, but a decoupled design.
Its main purpose is to achieve decoupling between objects having dependency by means of "third party" (i.e. IOC containers in Spring) (IOC easy to manage objects, you can just use),
thereby reducing the coupling between the code degree. IOC is a principle, instead of a pattern, the following modes (but not limited to) the IoC principle achieved.
Spring IOC container are like a factory, when we need to create an object, you can just configure the configuration file / annotation can be, no need to consider how objects are created out of.
IOC container is responsible for creating objects, connect objects together, configure these objects, and to process the entire lifecycle from the creation of these objects until they are completely destroyed.
In a real project a Service class if there are hundreds or even thousands of class as its bottom, we need to instantiate the Service, you may want to find out every time the constructor of this Service all the underlying class,
which could put people crazy. If the use of the IOC, you only need to configure, and where necessary quoted on the line, which greatly increases the maintainability of the project and reduce the development effort.
About Spring IOC understanding, I recommend a look at this almost know the answer, very good.
? Inversion of Control how to understand it give an example:
the object a dependent object b, when the object is a need to use the object must be to create their own b. But when the system is introduced into the vessel IOC, b between the object and the object a direct link lost.
This time, when the object is a need to use the object b, we can specify the IOC container to create an object b injected into a subject in. "
The process of obtaining a target dependent objects b by the proactive behavior becomes a passive behavior, control reversal, which is to control the origin of the reversal of the name.
DI (Dependency Inject, dependency injection), the control is reversed to achieve a design pattern, dependency injection is to instance variables that are passed to the object.

 

Guess you like

Origin www.cnblogs.com/zhncnblogs/p/10949453.html