[Spring IOC container loading process]

Spring IOC container loading is divided into the following steps:

  1. 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.

  2. Load the configuration file: After locating the configuration file, read the configuration file through BeanDefinitionReader and load the Bean definition information into BeanDefinitionRegistry.

  3. 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.

  4. Preprocessing BeanDefinition: After instantiating BeanFactory, some preprocessing needs to be done on BeanDefinition , such as parsing Bean dependencies, initializing placeholders, etc.

  5. 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 .

  6. 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.

  7. 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.

  8. 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.

  9. 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.

おすすめ

転載: blog.csdn.net/java_wxid/article/details/132890045