Service startup is the answer to Spring source design;

When I first learned the SpringBoot framework, I started the service for the first time, and what ghost did I call out? Only a few simple steps of configuration, a few core annotations, you can quickly achieve the construction and operation of the project;

Although the migration from the Spring framework to the SpringBoot framework will have a lot of discomfort in the early stage, the better framework will be quickly recognized, thus becoming the mainstream technology selection;

For most frameworks or components, the easier it is to use, the more complex the internal packaging strategy;

For example, when the Spring framework was updated to the SpringBoot version, the simplicity of its usage and the complexity of the internal packaging have become in sharp contrast; When it comes to the SpringCloud microservices framework, its packaging logic is ridiculously complex;

For the development of the server, it is impossible to bypass the deep learning of the Spring framework, if you simply stand in the perspective of source reading, it is recommended to read the SpringBoot startup process first, and then appropriately expand other source code blocks;

First of all, let’s talk about the basic idea of reading the source code, starting from a minimalist case, around the core API in the case as the entry point, through the breakpoint debugging of the source code logic, so as to experience the principle of its design;

Read the source code of SpringBoot, you can take the service startup method as the entry point, and then continuously analyze the core APIs and design principles involved in the startup process, and then analyze the abstract loading logic based on the specific startup log;

Before looking at the specific source code, it is also necessary to say the analysis ideas, Spring project, pay attention to the project and level of each API, and then analyze the relationship between the API, the core structure, attributes, methods, etc.;

In SpringBoot’s startup class, there are two core entry points, one is the construction method of the class, which completes the initialization action of a column; One is the startup method, which implements the creation and loading of the application context;

Construction Method:

Startup method:

It should be noted that because the SpringBoot service startup process involves too much source code, the above source code is only the core entry point of the listed part, and then around these key processes, analyze some common source code design;

In addition, the core version of the following source code: JDK-1.8, spring-5.2.4, spring-boot-2.2.5, there will be differences in source code under different versions;

When the service starts, according to the application type, the context created is judged, and the servlet-based web application is started here, so it also depends on the corresponding web server, which is Tomcat by default;

The core of the startup method lies in the creation, preparation, and refresh of the application context, and the application context is a very abstract description, which can be understood as the overall environment in which the application runs, which involves resource loading, configuration file assembly, management of running services, etc., and subsequent source code analysis is carried out around the API;

ApplicationContext: The core interface of the application context, in which all methods are in read-only mode, that is, they can only be accessed through the Get method;

ConfigurableApplicationContext: The context configuration extension interface provides the ability to configure the application context, maintain the lifecycle, and release related resources after shutdown.

AbstractApplicationContext: The abstract implementation of the context interface, the core API, which implements the public capabilities in the application context;

ConfigurableWebApplicationContext: Web application context configuration extension interface, providing the context configuration ability of web applications;

WebServerApplicationContext: the context of a Web service, the server that creates and manages web applications, and the Tomcat service is embedded in the process;

According to the API design of several cores of the application context, experience the design ideas of Spring source code, start from the top-level interface, continuously expand and add new methods, understand the logic of abstract implementation classes, and the specific APIs relied on by the service runtime;

What is a resource, it can be various types of files and configurations, byte input stream conversion, or URL resource positioning, Spring framework in the process of running, need to rely on the Resource interface to achieve access to the underlying resources;

Resource: The top-level interface of resource description, providing a series of methods, inheriting the InputStreamSource interface, and supporting the operation of converting resources into streams;

AbstractResource: An abstract implementation class for resource access, where the design principle is similar to the AbstractApplicationContext, providing the underlying implementation of the resource access method;

ResourceLoader: The encapsulated interface for resource loading, and the application needs to rely on this interface to obtain and access resources;

According to the requirements of different application scenarios, the implementation classes of the Resource interface are as follows: FileSystemResource file system resources, ClassPathResource resources under the classpath, InputStreamResource input stream resources, etc.

For the property and environment source code design system, refer to the above source code module, which is similar in idea, and is not described here;

The properties of the application and the parameters involved in the environment are described very much, the more direct means is to output through the method in the System class, as for how the information is loaded, methods are provided in the StandardEnvironment class, which can be viewed by breakpoints;

In applications based on the Spring framework, the Spring container is responsible for creating, assembling, setting properties, and then managing the entire life cycle of the object, called the bean object; The life cycle of a bean is very complex, and the process is roughly as follows: instantiation, property loading, pre- and post-initialization management, destruction;

BeanFactory: Factory class, the core capabilities of the Spring framework, the top interface of the bean container, provides a series of bean object access methods, is the basic support of IOC thinking and dependency injection;

ConfigurableBeanFactory: A configurable interface for bean containers, which is simply designed to allow plug and play inside the framework and access to the configuration methods of the bean factory;

AbstractBeanFactory: An abstract implementation class managed by Bean, which can view its internal doGetBean method, provide the acquisition logic of the Bean instance object, and execute the creation logic if it cannot be obtained;

When starting the SpringBoot project for the first time, the biggest question is that the Tomcat startup log can be seen, but there is no explicit server assembly, and the JAR package can be started directly, which simplifies the process by a big step;

WebServer: Web application server interfaces, such as the commonly used Tomcat, Jetty, Netty, etc., according to the application type, only provide three methods of starting, stopping, and obtaining ports, and are associated with the application context through the WebServerApplicationContext;

TomcatWebServer: The SpringBoot framework manages the core classes of the built-in Tomcat service, providing a layer of packaging for the management of the Tomcat lifecycle;

Tomcat: The lightweight Tomcat launcher in the Apache component, which provides the basic configuration of Tomcat, such as the default Port and HostName, as well as methods for lifecycle management, and the specific methods in the TomcatWebServer class are called in the API;

The event-driven model is a common decoupling method in complex processes, that is, through the event sending and listening to the two disassembly actions, to achieve step-by-step execution of the process, which is more vivid in the SpringBoot startup process and context loading;

ApplicationEvent: The basic abstract class of the application event, inherited from the EventObject class in the JDK, the concrete event will inherit the class, and the two core properties of the event source and the time of occurrence are declared internally;

ApplicationEventMulticaster: The top-level interface for application event broadcasting, which can broadcast specified application events to suitable listeners;

SimpleApplicationEventMulticaster: A simple implementation of the application event broadcast interface, you can break the multicastEvent method of the class to view the application event and its corresponding listener when broadcasting;

ApplicationListener: Application event listener interface, inherited from the EventListener interface in the JDK, Spring extends a variety of specific event listeners to achieve a variety of different scene requirements, such as the most common ConfigFileApplicationListener profile listener;

In SpringBoot engineering, the management strategy of the configuration file is very complex, there are internal programs to perform the loading configuration, there are also external integrated component configurations, of course, the core is the custom configuration of the project;

ConfigFileApplicationListener.Loader: The internal class of the configuration file listener implements the loading of the configuration source in the project, and its core logic is implemented in the Loader.load method, and the specific logic is completed by the relevant implementation class;

PropertySourceLoader: Configure the policy interface for loading, support multiple types of file configurations in Spring projects, such as yml, yaml, properties, xml, you need to select the corresponding loading implementation class through the extension of the file;

YamlPropertySourceLoader: Loads a .yml or .yaml type of file, a commonly used configuration file type in SpringBoot projects, and eventually converts to a collection of properties of Name and Value, which is described by the PropertySource abstraction class;

The power of the Spring framework also lies in the ability to integrate with other components simply and quickly, such as commonly used databases, caches, message queues and other types of components, analysis of the internal integration logic, will find a lot of similarities in principle, especially in the SpringBoot framework, the convention is greater than the configuration;

DataSourceAutoConfiguration: The automated configuration class for databases in SpringBoot projects, where Hikari is the default selection of the connection pool and is also known to be the fastest;

DataSourceProperties: The basic class related to data source configuration, in the DataSourceConfiguration configuration class, the data source object is created based on parameters;

HikariDataSource: The data source API in the Hikari connection pool component, which describes the specific information of the data source, such as configuration, connection pool, status, etc., and the specific database connection logic is completed inside the component;

Based on the principle of SpringBoot integrated database, you can extensively see: RedisAutoConfiguration configuration class of the Redis component; The KafkaAutoConfiguration class of the Kafka component and the RestClientAutoConfiguration class of the Elasticsearch component are similar in design principles;

Write at the end

From personal experience, if you want to read the source code design of the Spring framework, you need to build a large outline structure based on the application process, understand the common strategies and principles in the design, and then go deep into the details of the logic of a single module, so that it is easy to find the reading rhythm;

This article does not involve too much detail logic in the source code, but only from the service startup as an entry point, sorting out and developing the source code module with a more direct correlation, and describing the basic ideas of personal reading of Spring source code.