"Go" Spring learning---initialization process of SPRINGIOC container

Source: Internet
Author: User

"Original" https://www.toutiao.com/i6594400249429623304/

SPRINGIOC process of container initialization

In a nutshell, the initialization of the IOC container is initiated by the refresh () method, which marks the official launch of the IOC container.

Specifically, this launch includes Beandefinition's

Resouce three basic procedures for locating, loading and registering.

If we understand how to programmatically use the IOC container, we can clearly see the interface calls to the resource positioning and loading process. In the following sections, we will analyze the implementation of these three processes in detail. Before analyzing, it is important to remind the reader that spring separates the three processes and uses different modules, such as the corresponding Resourceloader, Beandefinitionreader and other modules, which are designed in such a way that It gives users more flexibility in tailoring or extending these three processes to define the initialization process that best suits their IOC container.

The first procedure is the resource positioning process. This resource positioning refers to the beandefinition resource location, which is accomplished by resourceloader through a unified resource interface, This resource provides a unified interface for the use of various forms of beandefinition. For these beandefinition forms of existence, I believe we will not feel unfamiliar. For example, the bean definition information in the file system can be abstracted using filesystemresource, and the bean definition information in the CLASSPATH can be used using the Classpathresource mentioned earlier, and so on. This positioning process is similar to the process of finding data in a container, just like water in a bucket to find water first.

The second process is the loading of a beandefinition. The onboarding process is to represent the user-defined bean as the data structure inside the IOC container, and the internal structure of the container is beandefinition. The detailed definition of this data structure is described below. Specifically, this beandefinition is actually an abstraction of the Pojo object in the IOC container, which allows the IOC container to easily manage the Pojo object, the bean, through this beandefinition-defined data structure. In the following chapters, we will conduct a detailed analysis of this onboarding process to give you a clearer picture of the process.

The third process is the process of registering these beandefinition with the IOC container. This process is done by invoking the implementation of the Beandefinitionregistry interface. This registration process registers the beandefinition that are parsed during onboarding to the IOC container. Through the analysis, we can see that the beandefinition is injected into a hashmap inside the IOC container, and the IOC container holds the beandefinition data through this hashmap. It is important to note that this is the IOC container initialization process, which generally does not include the implementation of bean dependency injection. In the spring IOC design, the bean-defined onboarding and dependency injection are two separate processes. Dependency injection typically occurs when the application first requests beans from the container through Getbean. One exception to note is that there is a pre-instantiated configuration when using the IOC container, through this pre-instantiated configuration (specifically, by defining the Lazyinit attribute in the information for the Bean), the user can take a small control over the container initialization process, This changes the dependency injection process of the bean that has the Lazyinit attribute set.

For example, if we set the Lazyinit property on a bean, then this bean's dependency injection is done in advance when the IOC container is initialized, without having to wait until the entire initialization is complete and the first time the Getbean is used.

After understanding the approximate outline of the IOC container initialization, we describe in detail how the resource positioning, onboarding, and parsing processes of the beandefinition are implemented during the initialization of the IOC container.

"Go" Spring learning---initialization process of SPRINGIOC container

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.