Spring source code--implementation steps of IOC container

Spring IOC (Inversion of Control) and DI (Dependency Injection)

IoC (Inversion of Control, Inversion of Control). This is the core of spring, throughout. The so-called IoC, for the spring framework, means that spring is responsible for controlling the life cycle of objects and the relationship between objects.
This is the development method advocated by Spring. All classes will be registered in the spring container, telling spring what you are and what you need, and then spring will take the initiative to give you what you want when the system is running at an appropriate time. , while also handing you over to other things that need you. The creation and destruction of all classes are controlled by spring, that is to say, the object that controls the life cycle of the object is no longer the object that refers to it, but spring. For a specific object, it used to control other objects, but now all objects are controlled by spring, so this is called inversion of control.

 

One of the key points of IoC is to dynamically provide an object with other objects it needs while the system is running. This is achieved through DI (Dependency Injection). For example, object A needs to operate the database. In the past, we always had to write code in A to obtain a Connection object. With spring, we only need to tell spring that a Connection is needed in A. As for how to construct this Connection, when to construct it, A does not need to know.

 

The relationship between IoC and DI is actually a description of the same concept from different perspectives . Because the concept of inversion of control is rather vague (it may only be understood as the level of container control objects, it is difficult for people to think of who will maintain the object relationship), so 2004 Martin Fowler, the master figure of the year, gave a new name: "dependency injection", relative to IoC, "dependency injection" clearly describes "the injected object depends on the IoC container to configure the dependent object"

 

Spring Bean loading process

  • The initialization of the container does several things:
  1. Resource positioning is to locate the xml file you configure
  2. Parse the resource file and parse it into the BeanDefinition defined by spring
  3. Registering the BeanDefinition is actually equivalent to putting the BeanDefinition in a CurrentHashMap
  • Dependency injection:
  1. Create Bean instance based on BeanDefinition
  2. Instances of injecting dependencies into beans

 

Below is the process

 

 

This interface system is centered on BeanFactory and ApplicationContext . BeanFactory is the most basic interface of the IOC container. On the one hand, ApplicationContext inherits the BeanFactory system: ListableBeanFactory and HierachicalBeanFactory, and has the basic functions of the BeanFactory IOC container. Added more advanced IOC container features.

 

 

 

The process of Spring in the project

First, the Web project uses Spring to initialize the IOC container by configuring org.springframework.web.context.ContextLoaderListener in web.xml

 

 

ContextLoaderListener inherits ContextLoader and implements the ServletContextListener interface. When the Server container (generally referred to as tomcat) starts, it will receive an event initialization. The initWebApplicationContext method is in the org.springframework.web.context.ContextLoader class. It does several things: first, it determines whether the WebApplicationContext has been registered in the servletContext, and if so, throws an exception to avoid repeated registration. Then it is to enable the log and start the timer. Then a series of complicated things are performed, setting the ServletContext as the attribute of XmlWebApplicationContext, so that the spring file configured in web.xml can be read through XmlWebApplicationContext, and then the instantiation of the above IOC container and bean injection are performed.

 

The above diagrams and brief descriptions of Spring can still be seen, but the source code of Spring is not so good to read. Here is a suggestion for you: follow the code along the main line, and then look back at the auxiliary code. Because Spring is a very mature framework, it needs to consider many issues in the coding process, such as the startup of the IOC container, we only need to know how it locates Resource, how to parse BeanDefinition, and how to register. Then in this process, there will be a lot of considerations based on security, performance, expansion, etc. In addition, Spring uses a lot of design patterns, which will produce a lot of redundant code in our eyes, and we don't know what to do. Therefore, when you read the source code a few times before, you should be able to ignore these codes and follow the mainline code and keep reading. When you figure out the process, go back and look at some of the previous auxiliary codes.

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326415241&siteId=291194637