Spring IOC trace part of manual framework ①

Posted by chaking on Sun, 05 Apr 2020 00:43:31 +0200

In the last section, we talked about the preparation of the basic environment. From this section, we officially enter the source code disassembly phase. There is a large amount of information. Please take out the notebook and record it

If you have any questions, please add QQ group: 77174608 to discuss.

Don't talk much nonsense, enter the tracking link.

First let's look at several constructors of ClassPathXmlApplicationContext class

Found that it overloaded so many constructs. Then we find the one that works.

Generally speaking, the one with the most parameters is basically

As you can see, all constructors will eventually get to it.

I mentioned the child and parent containers here. I won't talk about them for the time being. We will intersperse the following articles. Child parent container is an important concept in spring. For example, spring MVC is a typical application of child parent container. I'm not going to talk about it here

At the beginning, it depends on how he parses the configuration file (please follow the specific process by yourself. It's very simple, and I won't go over it here. This article focuses on the whole process)

When you get the path, you go in

if (refresh) {
   refresh();
}

This paragraph. This section is the core of the whole container. Let's go in and see what it does

It calls the parent class AbstractApplicationContext. refresh() method

I have some big comments here. In the following articles, we will explain each key node one by one.

@Override
public void refresh() throws BeansException, IllegalStateException {
   // Object lock, mainly to prevent the creation of multiple container objects. As for why, you should understand
   synchronized (this.startupShutdownMonitor) {
      // Record the start time and verify that all necessary variables are loaded...
      prepareRefresh();

      // High energy ahead... Please fasten your seat belts. Ready to start
      // Create a BeanFactory object.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Here we load some system level parameters for spring. The following articles will explain
      prepareBeanFactory(beanFactory);

      try {
         // Some operations of post-processing, methods reserved by spring. We can inherit and copy this method. Do something after initialization
         postProcessBeanFactory(beanFactory);

         // Some processors are called here
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register Bean's interceptor
         registerBeanPostProcessors(beanFactory);

         // Something to initialize the message source.
         initMessageSource();

         // Initialize event listener
         initApplicationEventMulticaster();

         // Spring reserves to initialize some Bean actions of other containers
         onRefresh();

         // Register listener
         registerListeners();

         // Instantiate non lazy loaded bean s
         finishBeanFactoryInitialization(beanFactory);

         // Publish corresponding events
         finishRefresh();
      }

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

         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();

         // Reset 'active' flag.
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Release something..
         resetCommonCaches();
      }
   }
}

So far, this chapter is over. Starting from the next chapter, I will go into every detail of Bean container initialization.  

Topics: Spring