Spring (2): IOC control inversion (also known as: dependency injection DI)

This is a people suddenly see the light of blog .......

 

A, Spring IOC introduced (Inversion of Control, also called dependency injection DI)

IoC role : coupling between objects in order to solve the problem of high

1.1 What is Inversion of Control

Is simply decomposing the object into a complex system of mutual cooperation, these objects through after encapsulation, the internal implementation is transparent to the outside, thereby reducing the complexity of the problem, can be reused and extended (that is, by "third party" decoupling between objects having dependency )

Image Analysis: Since the introduction of an intermediate position of "third party (IoC container)", so that A, B, C, D of the four objects is no coupling relationship, "thanks to third rotation between the gear", i.e. control of all objects to all "third party" the IoC container, similar to the " adhesive " to all objects bonded together play a role.

Summary : not before the introduction of IOC, the object A depends on object B, then object A to a point when you must take the initiative to create an object B or B using the object has been created in the initialization or run, A get B or not in control of their own hands on . After the introduction of IOC, objects A and B lost direct contact, when A needs B, IOC container will automatically create an object B injected into the local needs of the object A , B A get control over passive - so called inversion of control.

---- from

1.2, IoC alias: dependency injection (DI)

What is Dependency Injection :

IOC container during operation is dynamically injected into some dependency object dependency injection (DI) and reverse control (IOC) are described from different perspectives one thing - IOC container by introducing, using dependencies injection way, decoupling between objects.

---- from "Spring Inside"

 

1.3 Technical IOC container to achieve

IOC basic technology is the " reflection (the Reflection) " programmer, you can see the reflection of knowledge blogger this blog: reflective learning . The so-called reflection layman's terms is based on the class name given (character string) to dynamically generate the object, so that the object is generated when the decision was what kind of object.

understanding:

IOC container can be similar to the operation mode factory mode sublimation, IOC container can be regarded as a plant, but the plant to produce the given object is defined in the configuration file, and then using the generated class name reflection profile corresponding Object.

From the implementation point of view, IOC is put before the factory method in hardcoded object generation, instead defined by the configuration file, which is the subject of both generating plants and independent split open (and the difference between factory mode), the purpose is increase flexibility and maintainability (so called upgrade the factory model )

1.4, using the framework of the IOC

IOC Technical main products: Spring Framework

The disadvantage of using the IOC: the container is introduced to third step of generating complex objects, generating object IOC container by way of reflection, the reflection degrades performance

 

Two, Spring IOC source code analysis

pending upgrade....

 

Recommended Reading 1: https://yikun.github.io/2015/05/29/Spring-IOC%E6%A0%B8%E5%BF%83%E6%BA%90%E7%A0%81%E5% AD% A6% E4% B9% A0 /

Recommended Reading 2: https://javadoop.com/post/spring-ioc

 

Previous: the Spring AOP Aspect Oriented Programming

Next: the Spring Framework insight into

 

  Reference: "Inside the Spring"

Published 52 original articles · won praise 116 · views 50000 +

Guess you like

Origin blog.csdn.net/RuiKe1400360107/article/details/103597460