Spring's Srping IOC running sequence diagram

Load XML file - create IOC container sequence diagram

Source code understanding

First you need to find the entrance. The entry point for the IOC container to initialize and run.

review

What is Spring IOC

IOC (Inversion Of Control) inversion of control, the so-called inversion of control, is to create and depend on the objects that need to be implemented in our code. Reverse to the container to achieve. At the same time, a description is needed to let the container know the objects that need to be created and the relationship between objects. That is, configuration files.

Find the source code entry

1. About BeanFactory

BeanFactory: The top-level interface class defines the basic specifications of the IOC container.
Three important subclasses: ListableBeanFactory, HierarchicalBeanFactory and AutowireCapableBeanFactory
The final default implementation class: DefaultListableBeanFactory
Spring has its own functions in the process of object transfer and transformation during operation. Such as the ListableBeanFactory interface, indicating that the list Bean can be initialized

Analyze according to the sequence diagram

how to find the entrance

Through the use and preliminary understanding of Spring, we know that the initialization of Spring starts from DispatcherServlet. We can find that there are many methods starting with init in Spring. As the name suggests, these methods are prepared for Spring initialization. Through its inheritance relationship, we can find the original init() method in its parent class.

We can find a method named initServletBean () in HttpServletBean.

Pay attention to the code circled in red, combined with the log output from the console when we use Spring in the project.

We found the entrance without any problem. (Continuously updated,,,)

---------------------------------------------------2019-06-05---------------------------------------------------------------------------------------

The entrance has been found, and then slowly unravel the "veil" of the mysterious IOC container

In the initservletBean method, we can find that the initWebApplicationContext method is called, let's look at this method

There is a method called configureAndRefreshWebApplicationContext

The refresh() method is called in this method. We can simply understand that the refresh method will be called during the Spring startup process. Let's pay more attention to this method at this time.

The process of IOC container initialization

The process of container startup can be roughly summarized as three steps: positioning, loading and registration. That is, locate our configuration file, load the configuration file and register according to the configuration file

Taking ApplicationContext as an example, we can start loading the xml file in main. This will help us understand the IOC container.

By tracing its construction method, we found that the refresh() method was called, which is the starting point of our sequence diagram. Through the setConfigLocations method, we can also find that the configuration file is passed in the form of a string array. This can be used as a starting point for us to learn the IOC container, that is, locate the configuration file.

According to the sequence diagram, we can learn and understand the initialization process of the IOC container step by step.

2019-06-08: More detailed IOC running sequence diagram

Guess you like

Origin blog.csdn.net/GoNewWay/article/details/90038934