Spring IOC container loading is divided into the following steps:
-
Locating the configuration file: First, you need to locate the Spring configuration file . Spring provides a variety of methods, such as locating through ClassPath, FileSystem, etc.
-
Load the configuration file: After locating the configuration file, read the configuration file through BeanDefinitionReader and load the Bean definition information into BeanDefinitionRegistry.
-
Instantiate BeanFactory: Convert Bean definition information into Bean instances . At this time, there is no Bean instantiation in the container, but the Bean definition information is registered in BeanFactory.
-
Preprocessing BeanDefinition: After instantiating BeanFactory, some preprocessing needs to be done on BeanDefinition , such as parsing Bean dependencies, initializing placeholders, etc.
-
Register BeanPostProcessor: The Spring IOC container provides the BeanPostProcessor interface, which allows some customized processing before and after Bean instantiation . All classes that implement the BeanPostProcessor interface need to be registered in the container .
-
Instantiate non-lazy-loaded singleton beans: After the container is instantiated, the container will immediately instantiate all non-lazy-loaded singleton beans . These beans will be created when the container starts and placed in the singleton bean cache pool.
-
Register a listener: Before a Bean is instantiated, we can register a listener through the event mechanism provided by Spring to listen for Bean instantiation, destruction and other events.
-
Initialize the remaining beans: After the container is instantiated, it will wait for requests to initialize the remaining beans . It should be noted that lazy-loaded beans will only be initialized the first time they are used.
-
Container startup is completed: After the container completes the above steps, the Spring IOC container is started successfully , and the previously registered Bean instance can be obtained through the getBean() method.