preface

After the previous preparations, it is time to enter refresh, the core code.

The source code

public void refresh(a) throws BeansException, IllegalStateException {
    / / lock
    synchronized (this.startupShutdownMonitor) {
        StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

        // Prepare some context
        prepareRefresh();

        / / get prepared after the beanFactory DefaultListableBeanFactory
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        Prepare the BeanFactory / /
        prepareBeanFactory(beanFactory);

        try {
            // Allows post-processing of bean factories in context subclasses.
            postProcessBeanFactory(beanFactory);

            StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");

            // Call the post-processor registered as beanFactory in the context
            // Is the bean that implements BeanFactoryPostProcessor
            invokeBeanFactoryPostProcessors(beanFactory);

            // Register the post-processing Bean into the container
            // The Bean that extends the BeanPostProcessor
            registerBeanPostProcessors(beanFactory);
            beanPostProcess.end();

            // Initialize the message source for this context.
            // Initialize the internationalization tool MessageSource
            initMessageSource();

            // Initializes the event broadcaster for this context.
            initApplicationEventMulticaster();

            // Initialize other special beans in a specific context subclass. Subclasses can implement themselves
            onRefresh();

            // Check and register listeners.
            registerListeners();

            // Instantiate all remaining (non-lazy initialization) singletons.
            finishBeanFactoryInitialization(beanFactory);

            // Last step: Complete the refresh of this context, call the onRefresh () method of LifecycleProcessor and publish
            finishRefresh();
        }

        catch (BeansException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                        "cancelling refresh attempt: " + ex);
            }

            // Destroy the created singleton to avoid resource hanging.
            destroyBeans();

            // Reset the active state
            cancelRefresh(ex);

            // Propagate exception to caller.
            throw ex;
        }

        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...resetCommonCaches(); contextRefresh.end(); }}}Copy the code

Source code is a lot of dense, source code with corresponding annotations.

The following is based on the process, step by step in-depth source code, understand what each step is to do.

Container refresh preparation

The prepareRefresh step focuses on preparing some context information. No more introductions.

obtainFreshBeanFactory

Let’s focus on initializing the BeanFactory:

Enter the source code:

Debug shows that the refreshBeanFactory() step is called Org. Springframework. Context. Support. GenericApplicationContext# refreshBeanFactory implemented method.

Continue to follow up

The checksum check found a refreshed state.

RefreshBeanFactory methods also have another implementation is in AbstractRefreshableApplicationContext, just my breakpoint did not break into, this also with a look.

protected final void refreshBeanFactory(a) throws BeansException {
    // Destroy the Bean and close the BeanFactory if it exists
    if (hasBeanFactory()) {
        destroyBeans();
        closeBeanFactory();
    }
    try {
        // Create a default BeanFactory
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        beanFactory.setSerializationId(getId());
        // Set the properties
        customizeBeanFactory(beanFactory);
        // Load the Bean information
        loadBeanDefinitions(beanFactory);

        this.beanFactory = beanFactory;
    }
    catch (IOException ex) {
        throw new ApplicationContextException("I/O error parsing bean definition source for "+ getDisplayName(), ex); }}Copy the code

It creates the BeanFactory and loads the BeanDefinition. But I have not cut off to the moment, so I will introduce in detail after meeting later.

conclusion

This article provides a brief overview of the Refresh process and covers the first two parts:

  1. The prepareRefresh prepares context information
  2. ObtainFreshBeanFactory Initializes the BeanFactory

Related to recommend

  • Spring source code 08: Register the configuration class
  • Spring source learning 07: ClassPathBeanDefinitionScanner
  • Spring source study of 06: AnnotatedBeanDefinitionReader