Analysis of Spring source code-understanding of Spring core classes (1)

Preface

Experts fly over, free articles, do not spray if you don’t like it, welcome to communicate, please give praise if you like it.

I believe that every Java programmer has used the Spring framework. Spring is not only used for server-side development, but from the perspective of simplicity, testability, and loose coupling, most Java applications can benefit from Spring. Today’s The Spring Family Bucket is popular all over the world and is loved by programmers. This series of articles will take you step by step into the core of Spring.

[Reminder] Use IDEA tool to import Spring package to analyze source code. Of course, you can also download Spring source code from Github for analysis, which is better. address:https://gitee.com/mirrors/Spring-Framework/tree/v5.0.2.RELEASE/

Composition of Spring

The Spring framework adopts a layered architecture and is divided into multiple modules according to different functions. These modules can be roughly divided into Data Access/Integration, Web, AOP, Aspects, Messaging, Instrumentation, Core Container and Test. The following is the Spring architecture Figure

Insert picture description here

Data Access/Integration

Data access/integration layer, including JDBC, ORM, OXM, JMS and Transactions modules

  • JDBC module: support for JDBC, providing a JDBC abstraction layer to simplify Spring JDBC operations
  • ORM module: support for ORM framework integration, and develop object-relational mapping API, such as: JPA, JHibernate, MyBatis provide integration.
  • OXM module: OXM is an abbreviation of Object-to-XML-Mapping, which is an abstract layer implementation of XML and object mapping.
  • JMS module: supports Java message service, and contains functions for production and consumption of information.
  • Transactions: Support for things, support programmatic and declarative transaction management implementation

Web module

Spring's Web layer includes Web, Servlet, Struts and Portlet components

  • Web module: Provides basic Web development integration features, such as multi-file upload function, initialization of IOC container through Servlet or Listeners, and Web application context.
  • WebMVC module: is a Web-Servlet module that implements Spring MVC (model-view-Controller) web applications.
  • WebSocket module: It is mainly a protocol for full-duplex communication with the Web front end
  • WebFlux module: is a new non-blocking functional Reactive Web framework that can be used to build asynchronous, non-blocking, event-driven services, and has very good scalability

AOP and device support

Composed of 3 modules: spring-aop, spring-aspects and spring-instrument

  • AOP: Implement aspect-oriented programming, based on JVM's dynamic proxy,
  • Aspects: The spring-aspects module is integrated from the AspectJ framework, mainly to provide a variety of AOP implementation methods for Spring AOP
  • Instrument: A support module for AOP, which is designed based on "java.lang.instrument" in JAVA SE. When the JVM is started, a dynamic proxy is generated, and the bytes of the class are dynamically modified to realize the functions of AOP.

Messaging

It is a new module added from Spring 4, and its main responsibility is to integrate some basic messaging applications for the Spring framework.

Core Container

Core container, composed of spring-beans, spring-core, spring-context and spring-expression (Spring Expression Language, SpEL)

spring-core and spring-beans modules: are the core modules of the Spring framework, including Inversion of Control (IOC) and Dependency Injection (DI)

spring-context module: Built on the core module, he extended BeanFactory and added Bean life cycle control, framework event system and resource loading functions for her.

spring-context-support module: It is an extended support for Spring IOC container and IOC sub-container.

The spring-context-indexer module is Spring's class management component and Classpath scanning.

spring-expression module: It is an extension module of the unified expression language (EL), which can query and manage running objects, and it is also convenient to call object methods, manipulate arrays, collections, etc.

Test test module

The spring-test module mainly provides support for testing.

Spring IOC core class

IOC: Inversion of Control, which means that the creation and management of objects is handed over to Spring's IOC container for implementation, which means that we need to pass the classes that we need to IOC container management to Beans through XML configuration or annotation programming. Registration, and describe the dependencies between objects and objects. When the program starts, Spring will load the configuration file through the IOC container factory class, parse the configuration file, then register the Bean in the container, and finally consider whether to create an instance of the corresponding Bean.

DI: Dependency injection, the Spring container will actively inject another Bean that a Bean depends on. Since the IOC container is responsible for creating and managing the Bean, does it need to help us maintain the dependency between the Bean and the Bean? What?

IOC Container Factory

Therefore, there is a very key thing here, that is, Spring's IOC container factory.

Insert picture description here
  • ListableBeanFactory: This interface is an extension of BeanFactory, which allows BeanFactory defined by preloaded beans to implement this interface. Its purpose is to enable the BeanFactory that implements it to enumerate all Beans (that is, to obtain multiple Beans)
  • HierarchicalBeanFactory: The HierarchicalBeanFactory interface is an extension of BeanFactory. All container classes that have a hierarchical system (the kind with a parent BeanFactory) should implement this interface, that is, support for parentFactory is added on the basis of the functions defined by BeanFactory
  • ConfigurableBeanFactory: ConfigurableBeanFactory defines the configuration of BeanFactory and inherits the two interfaces of HierarchicalBeanFactory and SingletonBeanRegistry, which means that it inherits both hierarchical and singleton class registration functions.
  • ConfigurableListableBeanFactory: Provides bean definition parsing and registration functions, and then pre-loads the singleton (to solve the circular dependency problem). Inherited to ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory three interfaces. Own Bean
  • DefaultListableBeanFactory: is the core part of the entire bean loading, and is the default implementation of Spring registration and loading of Beans. DefaultListableBeanFactory inherits AbstractAutowireCapableBeanFactory and implements ConfigurableListableBeanFactory and BeanDefinitionRegistry interfaces
  • ApplicationContext: This container factory also inherits EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver interfaces. In addition to implementing the basic functions of IOC, it also extends internationalization support, resource access, event publishing, and more.
  • AliasRegistry: Defines the basic operations for adding, deleting, and modifying alias.
  • SimpeAiasRegitry: Use map to cache alias and implement AliasRegistry interface
  • BeanDefinitionRegistry: defines various additions, deletions, and modifications to BeanDefinition. BeanDefinition is an encapsulation of Ben
  • SingletonBeanRegistry: used to register and obtain simple interest beans
  • DefaultSingletonBeanRegistry: Implementation of SingletonBeanRegistry.
  • FactoryBeanRegstrySupport: Added special processing functions for FactoryBean on the basis of DefaultSingletonBeanRegistry

XmlBeanFactoryInherited from the DefaultListableBeanFactory class, the XML configuration is loaded through XmlBeanDefinitionReader. The main user is responsible for reading the Bean's xml definition from the XML configuration file, and then encapsulating and registering the BeanDefinition. For registering and obtaining beans, the methods inherited from the parent class DefaultListab leBeanFactory are used to implement.

ClasspathXmlApplicationContext

ClasspathXmlApplicationContext is also a member of the IOC container factory. By default, the XML configuration is obtained from the classpath, and XmlBeanDefinitionReader is used for parsing and configuration. This is a simple, one-stop convenient ApplicationContext.

XmlWebApplicationContext is an IOC container factory used in the web. It also obtains its configuration from an XML document and is parsed by XmlBeanDefinitionReader. By default, the configuration will obtain the root context from "/WEB-INF/applicationContext.xml"

Insert picture description here

XmlBeanDefinitionReader

XmlBeanDefinitionReader is used to read the Bean defined by XML. To delegate the actual XML document reading to the implementation of the BeanDefinitionDocumentReader interface, many classes are involved in the whole process:

Insert picture description here
  • ResourceLoader: Resource (XML configuration file) loader, returns Resource object according to the given configuration file address
  • BeanDefinitionReader: Defines the reading of resource files and encapsulates the function of BeanDefinition
  • EnvironmentCapable: Used to tune the Environment environment object, the environment object has configuration.
  • DocumentLoader: Convert resource files into Document document objects
  • AbstractBeanDefinitionReader: A concrete realization of the functions of EnvironmentCapable and BeanDefinitionReader
  • BeanDefinitionDocumentReader: defines the function of reading Document and registering BeanDefinition, the default implementation is DefaultBeanDefinitionDocumentReader
  • BeanDefinitionParserDelegate: defines the method of parsing XML elements

Here can summarize the process of reading the XML configuration file:

  1. Load the configuration file to Resource through ResourceLoader
  2. Convert Resource to Document object through DocumentLoader
  3. The Document is parsed through DefaultBeanDefinitionDocumentReader, and BeanDefinitionParserDelegate is called to parse XML elements.

BeanDefinition

BeanDefinitionIt is the definition object of Bean in Spring, which describes the various attributes of Bean and the relationship between Beans, and BeanDefinitionReaderit is used to complete the analysis of Bean. The inheritance system is as follows:

Insert picture description here

This article has a brief understanding of the inheritance system (part) of the Spring container. In the next chapter, we will track the loading of the Spring container and analyze what has been done during the Spring boot process throughout the entire process.