This is the fifth day of my participation in Gwen Challenge

What Spring’s IoC container does, as shown in the figure below, is load Configuration Metadata (usually Configuration information in XML format) in a way that binds system-wide objects, eventually assembling a usable lightweight container-based application.

The IoC container of Spring can basically be divided into two stages, namely container startup stage and Bean instantiation stage, according to a similar process, as shown in the figure below.

Spring’s IoC container takes full advantage of the different characteristics of the two implementation phases, adding container extension points in each phase so that we can add custom extension logic according to the needs of specific scenarios.

1. Container startup

When the container is started, Configuration MetaData is first loaded in some way. For most cases, containers need beandefinitionReaders to parse and analyze loaded Configuration MetaData. The parsed information is marshalled into the appropriate BeanDefinitionRegistry, and container startup is complete.

Overall, the work done in this phase can be considered preparatory, with a greater emphasis on the collection of object management information. Of course, some validation or supporting work can also be done at this stage.

2.Bean instantiation phase

After the first phase, all bean definition information is now registered with the BeanDefinitionRegistry as BeanDefinition. The second-phase activity is triggered when a requester explicitly requests an object through the container’s getBean method, or when the dependency container needs to implicitly invoke the getBean method.

In this phase, the container first checks to see if the requested object has been initialized before. If not, the requested object is instantiated and its dependencies are injected based on the information provided by the registered BeanDefinition. If the object implements some callback interface, it is also assembled according to the callback interface requirements. When the object is assembled, the container immediately returns it to the requester for use. If the first stage is just to assemble the production line according to the drawings, then the second stage is to use the assembled production line to produce specific products.