Overview of the core technology IOC of the Spring5.0 source code learning series (3)

Overview of the core idea of ​​the Spring5.0 source code learning series IOC (3)


Tip: Spring's official website gives a more detailed introduction to Spring's main technologies. For details, refer to the spring core document of Spring's official website . IOC implementation is a core function of springframework. This blog provides a more general description of spring ioc, which is only used before learning the source code. Popular science


Preface

Reminder: Spring framework source code is a better source code in the java field. The reason is that the framework design is very good and the framework is flexible. This is one of the reasons why the spring framework is popular. The Ioc (inversion of control) module and aop are two core functions of springframework. Ioc and aop are a kind of technical ideas, which have been put forward before the development of the springframework framework, but they are more theoretical, and spring has done a good implementation of these two ideas at the technical level (java field)


Tip: The following is the content of this article. The following case is for reference. It is only used as a popular science before learning the source code of ioc. Because the source code of ioc is more complicated, you need some necessary understanding before learning the source code. First of all, you are not familiar with the application. It is not understandable, so we must first learn to understand the implementation of spring ioc

1. What is IOC?

IOC: Inversion of controll (inversion of control/inversion control), ioc is a technical idea, which has been proposed before the development of the spring framework, and the spring framework implements the ioc technical idea very well

Understanding of inversion of control : Inversion of control (IOC) can be understood as the reversal of the way to obtain dependent objects . If there is reversal, there will be "forward rotation". The so-called "forward rotation" can be understood in this way, and "forward rotation" is the normal The method of obtaining objects, such as class A depends on class B, in class A to obtain class B, it is only necessary to new a B object, this is "forward rotation", this method of management and acquisition of dependent objects, or It is triggered by class A. The reverse situation is that the control of dependent objects is given to the spring ioc container, and the creation and management of objects is given to the ioc container. What objects are needed can be obtained directly through the ioc container.

The situation that does not depend on IOC: obtain dependent objects directly through new. In
Insert picture description here
the case of the Spring IOC container: the creation and management of all objects are controlled by the spring ioc container. What objects are needed directly to the ioc container, as shown in the figure:
Insert picture description here
Why is it called inversion of control?

  • Control: refers to the control, instance, and management of dependent objects
  • Reversal: The control of dependent objects is handed over to the external environment, which is the Spring IOC container

Second, the role of the IOC container

Quoting other blogs’ interpretation of the ioc interpretation: http://www.52codes.net/article/40150.html , it can be seen from the figure that the ioc container is actually used for decoupling, reflecting the idea of ​​oop
Insert picture description here

ps: In programs without IoC, we use object-oriented programming. The creation of objects and the dependencies between objects are completely hard-coded in the program.


Three, the difference between IOC and DI

DI: Dependancy Injection, Inversion of Control (IOC) and Dependency Injection (DI) actually describe the same thing, but from a different perspective

  • Inversion of Control (IOC) is from the perspective of the object, emphasizing that the control of the object is handed over (inverted to) the IOC container, which controls the instance and management of the object
  • Dependency injection (DI) from the perspective of the IOC container means that the IOC container injects dependent objects into the IOC container. If class A declares the properties of class B, the object of class B needs to be injected into class A when the ioc container starts

Four, Spring container overview

Go to the Spring official website to find the relevant documentation for the IOC container. I found that the official documentation is quite detailed. Although there is no analysis of the Spring framework source code, the IOC is also explained in detail. For details, please refer to the official documentation. This blog refers to the official documentation. Make a summary description:
Insert picture description here

Description of Spring ioc container on Spring official website:

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.

In fact, the meaning is similar to the previous explanation, which means that inversion of control (IOC) is actually inverting the control of dependent objects to the IOC container.

The picture on the official website describes the general process of spring container management objects:
Insert picture description here


5. How to use IOC container?

That is to say, the three steps described in the official document: the
Insert picture description here
translation is:

  • Configuration metadata
  • Example IOC container
  • Use IOC container

ok, follow the official documents to learn:

5.1, configuration metadata

The official website introduces two methods: that is based on annotations , and the other is based on java configuration .
Insert picture description here
Based on annotations, it is mainly the primary key provided by spring, such as the frequently used @Autowired primary key. For details, please refer to the official document
Insert picture description here

There are two methods based on java configuration. One is to write a @Configurationconfiguration class, and the other is to use the xml configuration file. For details, please refer to the official document :

Insert picture description here

@Configuration
public class AppConfig {
    
    

    @Bean
    public MyService myService() {
    
    
        return new MyServiceImpl();
    }
}
<beans>
    <bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>

Use mind map, simple drawing:
Insert picture description here

5.2, instance IOC container

An example of a Spring IOC container, the official website example is:

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- services -->

    <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>

Simply summarize:

Insert picture description here

5.3, use IOC container

After the IOC container is instantiated, the corresponding dependent objects can be obtained:
Insert picture description here


Six, Spring Bean overview

6.1 Overview of Beans in Spring

Bean overview: Bean in Spring refers to Beandefinition, and Beandefinition is an instance through the Spring container instance

6.2, the way of instance Bean

Insert picture description here

Insert picture description here
Official website code example: static factory instance

public class ClientService {
    
    
    private static ClientService clientService = new ClientService();
    private ClientService() {
    
    }

    public static ClientService createInstance() {
    
    
        return clientService;
    }
}

Factory instance:

public class DefaultServiceLocator {
    
    

    private static ClientService clientService = new ClientServiceImpl();

    public ClientService createClientServiceInstance() {
    
    
        return clientService;
    }
}

Seven, define SpringBean

7.1, define Spring Bean

Spring's official website shows how to define a Spring Bean:
Insert picture description here
This blog uses a mind map to summarize:
Insert picture description here

7.2, Bean scope

Insert picture description here

range description
Singleton (Singleton) (Default) singleton beanDefinition
Prototype Multiple instances, multiple instances of beanDefinition
Request Singleton, acting on the life cycle of HTTP requests
Session Singleton, the life cycle of the Http session
Application Singleton, applied to the ServletContext life cycle
Network socket (webSocket) Singleton, applied to Websocket environment

The legend of the singleton mode, the figure comes from the Spring official website
Insert picture description here

Prototype mode, also called multi-case mode, the legend comes from Spring official website:
Insert picture description here

7.3, Bean lazy loading mode

Lazy-initialized beans: The lazy-loading mode is that the bean is instantiated when it is called for the first time, not when the spring container is started, which is not turned on by default. (A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.), by changing the configuration lazy-init="true"


Eight, Spring life cycle

The life cycle of Spring is a more important aspect of the Spring framework, but there are more content, so this blog is just a brief description

Insert picture description here


Nine, Spring post processor

Post processor is a more important aspect of the Spring framework, but there are more content, so this blog is just a brief description
Insert picture description here


10. What is circular dependency?

Two or more classes call and depend on each other to form a closed loop. The Spring framework detects this scenario and throws BeanCurrentlyInCreationException, exposing the method of the object in advance
Insert picture description here

Insert picture description here


induction

Tip: Here is a summary of the article:
For example, the above is what we will talk about today. This article only briefly introduces the use of pandas, and pandas provides a large number of functions and methods that enable us to process data quickly and conveniently.

Insert picture description here

Guess you like

Origin blog.csdn.net/u014427391/article/details/109288905