SpringBoot principle in-depth and source code analysis

Posted Jun 28, 20205 min read

  1. SpringBoot principle in-depth and source code analysis

    The traditional Spring framework implements a Web service, you need to import various dependent JAR packages, and then write the corresponding XML configuration file. Compared with it, Spring Boot is more convenient, fast and efficient. So, how exactly does Spring Boot do this?

Next, we will conduct in-depth analysis of the dependency management, automatic configuration and execution processes of the Spring Boot framework

2.1 Dependency management

Question:(1) Why is it not necessary to specify the version when importing dependency?

In the Spring Boot entry program, the project pom.xml file has two core dependencies, namely spring-boot-starter-parent and spring-boot-starter-web. The relevant introductions of these two dependencies are as follows:

  1. spring-boot-starter-parent dependency

Find the spring-boot-starter-parent dependency in the pom.xml file in the chapter01 project. The sample code is as follows:

xml

org.springframework.boot

spring-boot-starter-parent<11./artifactId>

2.2.2.RELEASE

In the above code, the spring-boot-starter-parent dependency is used as the unified parent project dependency management of the Spring Boot project, and the project version number is unified as 2.2.2.RELEASE, which can be modified according to actual development needs

Use "Ctrl + left mouse button" to enter and view the underlying source file of spring-boot-starter-parent, and found that the underlying layer of spring-boot-starter-parent has a parent dependency spring-boot-dependencies, the core code is as follows

xml

org.springframework.boot

spring-boot-dependencies

2.2.2.RELEASE

../../spring-boot-dependencies

Continue to view the underlying source files of spring-boot-dependencies, the core code is as follows:

xml

<activemq.version>5.15.11</activemq.version>

<solr.version>8.2.0</solr.version>

<mysql.version>8.0.18</mysql.version>

<kafka.version>2.3.1</kafka.version>

<spring-amqp.version>2.2.2.RELEASE</spring-amqp.version>

<spring-restdocs.version>2.0.4.RELEASE</spring-restdocs.version>

<spring-retry.version>1.2.4.RELEASE</spring-retry.version>

<spring-security.version>5.2.1.RELEASE</spring-security.version>

<spring-session-bom.version>Corn-RELEASE</spring-session-bom.version>

<spring-ws.version>3.0.8.RELEASE</spring-ws.version>

<sqlite-jdbc.version>3.28.0</sqlite-jdbc.version>

<sun-mail.version>${jakarta-mail.version}</sun-mail.version>

<tomcat.version>9.0.29</tomcat.version>

<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>

<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>

As can be seen from the underlying source file of spring-boot-dependencies, this file uses the tag to perform unified version number management on some common technical framework dependency files, such as activemq, spring, tomcat, etc., all with Spring Boot 2.2. Version 2 matches the version, which is why pom.xml introduces the dependency file and does not need to mark the version number of the dependency file.

It should be noted that if the dependency file introduced by pom.xml is not managed by spring-boot-starter-parent, then when introducing the dependency file in pom.xml, you need to use the tag to specify the version number of the dependency file.

(2) Question 2:The main function of the spring-boot-starter-parent parent dependency starter is to carry out unified version management, so where do the JAR packages that the project depends on come from?

  1. Spring-boot-starter-web dependencies

    View spring-boot-starter-web dependent file source code, the core code is as follows

xml

org.springframework.boot

spring-boot-starter

2.2.2.RELEASE

compile

org.springframework.boot

spring-boot-starter-json

2.2.2.RELEASE

compile

org.springframework.boot

spring-boot-starter-tomcat

2.2.2.RELEASE

compile

org.springframework.boot

spring-boot-starter-validation

2.2.2.RELEASE

compile

tomcat-embed-el

org.apache.tomcat.embed

org.springframework

spring-web

5.2.2.RELEASE

compile

org.springframework

spring-webmvc

5.2.2.RELEASE

compile

It can be found from the above code that the main function of the spring-boot-starter-web dependency starter is to provide all the underlying dependencies required by the Web development scenario

That is why, when the spring-boot-starter-web dependency starter is introduced in pom.xml, web scenario development can be achieved without the need to import additional Tomcat servers and other web dependency files. Of course, the version numbers of these introduced dependency files are still managed by the spring-boot-starter-parent parent dependency.

In addition to the Web dependency starter described above, Spring Boot also provides related dependencies for many other development scenarios. We can open the official Spring Boot documentation and search for the "Starters" keyword to query the scenario dependency starter

![image-20191226111001207](./images/image-20191226111001207.png)

Listed some of the scene-dependent starters officially provided by Spring Boot. These dependent starters are suitable for different scene development. When you use them, you only need to import the corresponding dependency starters in the pox.xml file.

It should be noted that the Spring Boot official does not provide a scene starter for the technical framework developed for all scenarios. For example, the database operation framework MyBatis, Alibaba's Druid data source, etc., Spring Boot official does not provide a corresponding dependency starter. In order to make full use of the advantages of the Spring Boot framework, when the Spring Boot official did not integrate these technical frameworks, the development team where the MyBatis, Druid and other technical frameworks took the initiative to integrate with the Spring Boot framework to achieve their respective dependent starters, such as mybatis-spring-boot-starter, druid-spring-boot-starter, etc. When we introduce these third-party dependent launchers in the pom.xml file, remember to configure the corresponding version number

I just learned the "Java Engineer High Salary Training Camp" of Lagou Education, and I answered when I saw what I just learned. I hope Lagou can push me to the company I want to go to. Goal:Byte! !