This is the 7th day of my participation in Gwen Challenge
With the BeanFactoryPostProcessor now available to intervene in the startup phase of the container, we can explore the implementation logic for the next phase, the Bean instantiation phase.
The corresponding bean definition is not instantiated immediately after the container is started. The container now only has the BeanDefinitions of all objects to hold the necessary information that will be used during the instantiation phase. Only when a requester requests an object instance through the getBean method of the BeanFactory can the Bean’s instantiation phase be triggered. The getBean method of the BeanFactory can be explicitly called by the client object or implicitly called from within the container. There are two cases of implicit invocation:
1. For BeanFactory, object instantiation is lazily initialized by default. In general, when object A is requested and needs to be instantiated for the first time, the container instantiates the object on which object A depends if object B has not been instantiated before. In this case, the container calls the getBean method internally, which is implicit for this request.
2.ApplicationContext starts and instantiates all bean definitions. However, the ApplicationContext implementation still follows the two phases of the Spring container implementation process, except that it immediately calls getBean(), the instantiation method defined by all beans registered with the container, after the activities of the startup phase are complete. For details, see refresh()
The reason it is possible to trigger Bean instantiation is that Bean instantiation is triggered only when the getBean() method corresponding to a Bean definition is called for the first time, either implicitly or explicitly. The second call directly returns the first instantiated object in the container cache (except for the Prototype type). When the getBean() method finds that the bean has not been instantiated before, the specific object instantiation is done through the createBean() method. The instantiation process is shown in the following figure:
To be continued…