Spring's IOC summary (two)

1. What is IoC?

Ioc—Inversion of Control, that is, "inversion of control", a design idea. In Java development, Ioc means giving your designed objects to the container to control, instead of traditional direct control inside your objects.

1.1 Who controls whom and what?

The non-IOC design idea is to create objects directly inside the object through new, and the program actively creates dependent objects
. IoC has a special container to create these objects. That is, the Ioc container controls the creation of objects.
Who controls who? What does the
IoC container control object
control?
That is to control the acquisition of external resources (not just objects including files, etc.)

1.2 Why is it reversed, and in what ways?

Forward rotation: That is, we actively control in the object to directly obtain dependent objects.
Reverse: The container helps to create and inject dependent objects.
Why is it reverse?
Because the container helps us find and inject dependent objects,
what aspects of the objects are just passively accepting dependent objects ?
The acquisition of dependent objects is reversed

1.3 Legend of non-IOC object injection

Insert picture description here

2.IoC 和 IN

DI-Dependency Injection, or "dependency injection": The
dependency between components is determined by the container at runtime. To put it more vividly, the container dynamically injects a certain dependency into the component. The purpose of dependency injection is not to bring more functions to the software system, but to increase the frequency of component reuse and build a flexible and extensible platform for the system. Through the dependency injection mechanism, we only need to specify the resources required by the target and complete our own business logic through simple configuration without any code, without caring about where the specific resources come from and who implements them.
Keywords for DI:

  1. Who depends on whom: The application depends on the IoC container
  2. Why do we need to rely on: The application needs an IoC container to provide the external resources needed by the object
  3. Who injects whom: Obviously it is the IoC container that injects an object of the application, the object the application depends on
  4. What is injected: is to inject external resources (including objects, resources, constant data) required by an object

What is the relationship between IoC and DI?
In fact, they are descriptions of the same concept from different perspectives. Because the concept of inversion of control is relatively vague (may be just understood as the level of container control objects, it is difficult to think of who will maintain the object relationship), so the 2004 master Martin Fowler A new name is given: "dependency injection". Compared with IoC, "dependency injection" clearly describes "the injected object depends on the IoC container configuration dependent object".

A key point of IoC is to dynamically provide an object with other objects it needs while the system is running. This is achieved through DI (Dependency Injection).
For example: For
example, object A needs to operate a database. In the past, we always had to write code in A to obtain a Connection object. With spring, we only need to tell spring that a Connection is needed in A. As for how and when this Connection is constructed Structure, A does not need to know. When the system is running, spring will create a Connection at an appropriate time, and then inject it into A like an injection, thus completing the control of the relationship between each object. A needs to rely on Connection to run normally, and this Connection is injected into A by spring. That's how the name of dependency injection comes from.

How is DI implemented?
An important feature after Java 1.3 is reflection, which allows programs to dynamically generate objects, execute methods of objects, and change properties of objects when the program is running. Spring implements injection through reflection.

3. IoC initialization process

The process of IoC initialization is essentially the process of instantiating Beans, that is, the life cycle of Spring Beans. The general steps are:

  1. Resource positioning: refers to the resource positioning process of BeanDefinition. In layman's terms, it is to find the XML file that defines the Javabean information and encapsulate it into a Resource object
  2. Loading of BeanDefinition; the user-defined Javabean is represented as a data structure inside the IoC container, and the data structure inside this container is BeanDefinition
  3. Register these BeanDefinitions with the IoC container

Guess you like

Origin blog.csdn.net/xueguchen/article/details/108237492