The initialization process of the Spring IOC container

The initialization of the IoC container is the four processes of locating, loading, parsing, and registering the Resource containing BeanDefinition information. Finally, the bean we configured exists in the IoC container, that is, the memory with the beanDefinition data structure. There is no dependency injection of beans involved here, just the loading of bean definitions. But there are exceptions. There is a pre-instantiated configuration when using the Ioc container, that is, the lazyinit attribute is set in the bean definition, then the bean is pre-loaded when the Ioc container is initialized, and there is no need to wait until the Ioc is initialized for the first time. It will only be triggered when getBean. Among them, refresh() starts the initialization of the Ioc container.
image

The first process is the Resource positioning process.

This Resource positioning refers to the resource positioning of BeanDefinition, which is completed by ResourceLoader through a unified Resource interface. This Resource provides a unified interface for the use of various forms of BeanDefinition. I believe everyone will not be unfamiliar with the existence of these BeanDefinitions. For example, the Bean definition information in the file system can be abstracted using FileSystemResource; the aforementioned ClassPathResource can be used in the class path, and so on. This process is similar to the process of finding data in a container, just like filling water in a bucket to find the water first.

The second key part is the loading of BeanDefinition

The loading process represents the user-defined Bean as a data structure inside the IoC container, and the data structure inside this container is BeanDefinition. The detailed definition of this data structure can be seen below. In general, this BeanDefinition is actually an abstraction of the POJO object in the IoC container. This BeanDefinition defines a series of data to enable the IoC container to conveniently manage the POJO object, which is the Spring Bean. That is, BeanDefinition is the domain object of Spring. Below we will conduct a detailed analysis of this loading process, so that everyone can have a clearer understanding of the entire process.

The third process is the process of registering these BeanDefinitions with the IoC container.

This process is completed by calling the implementation of the BeanDefinitionRegistry interface. This registration process registers the BeanDefinition parsed during the loading process with the IoC container. It can be seen that the BeanDefinition is injected into a HashMap inside the IoC container, and the Ioc container holds these BeanDefinition data through this HashMap.

The whole process can be understood as the initialization process of the container.

The initialization of the container is achieved by refresh() of AbstractApplicationContext.

image




Guess you like

Origin blog.51cto.com/15082402/2644345