Springboot source code – bean loading (Part 1)

Time:2022-5-11

I’ll talk to you again, hey hey~

In the run () method startup process described in the previous section, there is a line of code:

refreshContext(context);

This line of code is today’s hero – it completes the loading of the bean. Its implementation is in the refresh() method of abstractapplicationcontext class, with the following code:

@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
    StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
    //Container status setting, initialization attribute setting, verification mandatory attribute
    prepareRefresh();
    //Get beanfactory
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    //Set some properties of beanfactory, add post processor, register the default environment beans
    prepareBeanFactory(beanFactory);
    try {
        //Empty method, leave a hook for the subclass and inject it into postprocessors
        postProcessBeanFactory(beanFactory);
        
        StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
        //Execute beanfactoryprocessor
        invokeBeanFactoryPostProcessors(beanFactory);

        //Beanpostprocessor registers into the container
        registerBeanPostProcessors(beanFactory);
        beanPostProcess.end();

        //Initialize the message source for the container
        initMessageSource();

        //Register event broadcaster for container
        initApplicationEventMulticaster();

        //Empty method, leaving the subclass to register other beans
        onRefresh();

        //Register the event listener and dispatch the events that have not been handled before
        registerListeners();

        //Initialize the remaining single instance beans
        finishBeanFactoryInitialization(beanFactory);

        //Initialize the cycle processor and publish the container startup event
        finishRefresh();
    }
        …………
        finally {
            //Clear cache
            resetCommonCaches();
            contextRefresh.end();
        }
    }
}

The above is the core process of spring container startup. In the next section, the focus comes~~

Recommended Today

[about learning programming] what we need to learn is programming, not programming language!

We are not only programmers, but also learners. What’s rare is how many people think they’re learning programming. You may be learning the programming language, not the programming itself Don’t be surprised by the statement that learning computer science (CS) is not studying computers. On the contrary, learning CS is the study of automatic problem […]