Why doesn't Spring Boot need to install additional Tomcat?

When I first came into contact with Spring Boot, most children should be as curious as me:

Why doesn't Spring Boot need to install additional Tomcat?

Why on earth? Let's start today's journey with curiosity.

open Previous section We built the tobebeter javaer project and found POM XML file, you can see a parent attribute in it. The code is as follows:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.6.1</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

What do you mean?

This means that our current Spring Boot project depends on the parent project Spring Boot starter parent. It's a bit like extensions in Java.

How to view spring boot starter parent What about the contents of the POM file?

If you are not sure where your Maven local warehouse is, you can execute the MVN help: effective settings command on the terminal.

You can lock the spring boot starter parent according to the groupId, artifactId and version of the parent The location of the POM file.

After opening with a text editor, you can roughly see the following:

  • The JDK version is defined as 1.8
  • The default encoding method of the project is UTF-8
  • Maven's compilation environment
  • And parent dependencies spring boot dependencies

According to the gourd drawing, we find spring boot dependencies in the same way POM file. You can see that a series of attributes and dependencies are defined, almost 2800 lines.

There are message queue dependency, commons toolkit dependency, database link dependency, HTTP link dependency, Spring family dependency, Web server dependency and so on.

It can be said that this is the version management center that the Spring Boot project depends on.

By default, the version management center configures all the basic environment versions required by the project. These versions will change with the upgrade of Spring Boot version. In other words, developers don't need to care about these trivial dependent versions anymore. Just give them to the housekeeper Spring Boot.

Spring Boot will help us select the most stable new version, which reflects the soul of the Spring Boot project: "agreement is better than configuration". You can configure it if you want, but it's not necessary. Just follow the Convention.

With this in mind, let's move on to POM XML file, which has a spring boot starter web dependency. This time, we directly press and hold the Ctrl key (macOS is the Command key) and click the left mouse button to jump to spring boot starter web POM's source file.

Part of the source code is as follows:

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.6.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.6.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.6.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.13</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.13</version>
      <scope>compile</scope>
    </dependency>

Spring web provides core HTTP integration, including some convenient servlet filters and Spring HTTP calls, which are used to integrate the infrastructure and technologies of other web frameworks (Hessian, Burlap).

Spring webmvc is an implementation of Spring MVC. Spring webmvc depends on spring web, so including it will indirectly add spring web without displaying add spring web.

Take a look at the pom file of spring boot starter Tomcat:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <version>2.6.1</version>
  <name>spring-boot-starter-tomcat</name>
  <dependencies>
    <dependency>
      <groupId>jakarta.annotation</groupId>
      <artifactId>jakarta.annotation-api</artifactId>
      <version>1.3.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-core</artifactId>
      <version>9.0.55</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <artifactId>tomcat-annotations-api</artifactId>
          <groupId>org.apache.tomcat</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-el</artifactId>
      <version>9.0.55</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-websocket</artifactId>
      <version>9.0.55</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <artifactId>tomcat-annotations-api</artifactId>
          <groupId>org.apache.tomcat</groupId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>

It can be seen from here that the default startup container of spring boot is tomcat, and the core of Tomcat is Jakarta annotation,tomcat-embed-core,tomcat-annotations-api,org.apache.tomcat.embed was all introduced through Maven.

The version of core is 9.0.55, the latest version on Tomcat's official website X version is 9.0.56, a higher version.

However, it doesn't matter. Download the src of 9.0.56 directly and see if it is roughly the same.

In contrast, it can be seen that the Tomcat introduced by Spring Boot is a little more streamlined and basically the same, which is the fundamental reason why Spring Boot does not need to install additional Tomcat.

Spring Boot's starter has already done it for us. This is also an important reason why Spring Boot is popular. It saves the time for developers to configure and focuses more on the implementation of business logic and performance optimization. As for those complicated configurations, just leave them to the Spring Boot housekeeper. As long as they are OK and do not need special customization, they can be used.

This article has been included in the open source column "the road to advanced Java programmers" of star 1.0k+ star on GitHub. The column is humorous, easy to understand, and extremely friendly and comfortable to java lovers 😄, The content includes but is not limited to core knowledge points such as Java foundation, Java collection framework, Java IO, Java Concurrent Programming, Java virtual machine, Java enterprise development (Git, SSM, Spring Boot).

Star becomes a better Java programmer by using this warehouse. You can click the link below to jump to the past star to witness this exciting moment.

https://github.com/itwanger/toBeBetterJavaer

The column is still on the GitHub Trending list (the daily list of Java classes), which makes the second brother finally feel the happiness of dominating the list!

Nothing makes me stay - except for purpose, even if there are roses, shade and quiet harbor on the shore, I am not a boat.

Keywords: Spring Boot Tomcat intellij-idea

Added by svgk01 on Tue, 04 Jan 2022 05:15:43 +0200