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) {

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.

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

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

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

         // Some processors are called here

         // Register Bean's interceptor

         // Something to initialize the message source.

         // Initialize event listener

         // Spring reserves to initialize some Bean actions of other containers

         // Register listener

         // Instantiate non lazy loaded bean s

         // Publish corresponding events

      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.

         // Reset 'active' flag.

         // Propagate exception to caller.
         throw ex;

      finally {
         // Release something..

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

Topics: Spring