[Project combat] Getting started with SpringBoot source code from 0-SpringBoot integrates Tomcat source code

1. SpringBoot provides support for a variety of containers

SpringBoot provides support for a variety of containers, including Tomcat, Jetty, Undertow, etc., and you can choose a suitable container to deploy applications according to actual needs.

Among them, Tomcat is a lightweight web server and application server, which can run on different operating systems such as Windows, Linux, and Mac OS. SpringBoot uses Tomcat as the web server by default, and the parameters of Tomcat can be configured through the server.tomcat.* properties in the configuration file.

In addition, SpringBoot also provides support for other containers, such as using Jetty as a web server, which can be achieved by adding spring-boot-starter-jetty dependencies in the pom.xml file.

2. SpringBoot integrates the Tomcat trilogy

The process of SpringBoot integrating Tomcat is very simple, mainly divided into the following steps:

2.1 Add Tomcat dependency

Add Tomcat dependencies in SpringBoot's pom.xml file:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

2.2 Configure the port number

In the SpringBoot configuration file, the port number can be configured through the server.port property.
For example, configure the port number as 8080:

server.port=8080

2.3 Deploy the application

SpringBoot provides a SpringApplication.run(...) method through which applications can be deployed.
The specific implementation is as follows:

@SpringBootApplication
public class MyApp {
    
    
  public static void main(String[] args) {
    
    
    SpringApplication.run(MyApp.class, args);
  }
}

The above is the process of SpringBoot integrating Tomcat.

3. SpringBoot integrates source code interpretation of Tomcat

The source code of SpringBoot integrating Tomcat is mainly divided into the following parts:

3.1 Server related configuration

In the SpringBoot configuration file, you can configure server-related parameters, such as port number, access path, etc., through the server attribute. Among them, the tomcat attribute can be used to configure Tomcat-related parameters, such as startup mode, port number, and so on.

The specific configuration is as follows:

server:
  servlet:
    context-path: /
  tomcat:
    port: 8080
    base-directory: /Users/username/projects/myproject/target/classes
    window-style: forward

3.2 Custom Servlet

SpringBoot provides a WebMvcConfigurer interface, which can be implemented to customize the Servlet.
The specific implementation is as follows:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    
  @Bean
  public ServletRegistration.Dynamic servletRegistration() {
    
    
    ServletRegistration.Dynamic servlet = ServletRegistration.Dynamic
        .create("myServlet", MyServlet.class);
    servlet.addMapping("/");
    return servlet;
  }
}

3.3 Deploy the application

SpringBoot provides a SpringBootServletWebServerFactory class, which can be implemented to customize the web server. The specific implementation is as follows:

@Configuration
public class TomcatConfig implements WebServerFactoryConfigurer {
    
    

  @Override
  public void configure(WebServerFactoryConfigurer configurer) throws Exception {
    
    
    configurer.withServlets(servletRegistration());
  }

  @Override
  public void afterPropertiesSet(WebServerFactory factory) throws Exception {
    
    
    // TODO Auto-generated method stub

  }
}

The above is the source code of SpringBoot integrating Tomcat.

4. How does SpringBoot provide support for multiple containers at the source code level

SpringBoot provides support for multiple containers at the source code level, mainly by providing different starters and related configurations. For example, SpringBoot provides starters such as spring-boot-starter-tomcat and spring-boot-starter-jetty, which are used to support Tomcat and Jetty as web servers respectively. In these starters, related dependencies and configurations are included, such as the port number of Tomcat, the context path of the application, etc. In addition, SpringBoot also provides the WebMvcConfigurer interface, which can be implemented to customize the configuration of the Web server. In short, SpringBoot supports a variety of containers by providing different starters and related configurations.

5. To integrate the source code of SpringBoot and Tomcat, you can refer to the following steps:

First, create a SpringBoot web project.
Add the Tomcat dependency in the pom.xml file.
Create an application.yml file under src/main/resources to configure Tomcat's port and context path.
Create a startup class under src/main/java, inherit SpringBoot's WebStarter, and rewrite the configure method.
In the startup class, use the @Bean annotation to create a TomcatServletWebServerFactory object and override the getServletContext method.
Finally, in the TomcatServletWebServerFactory object, use the @Bean annotation to create a TomcatServletWebServer object and override the addInitializers method.
Once the integration is complete, you can run the project.

Supongo que te gusta

Origin blog.csdn.net/wstever/article/details/129978268
Recomendado
Clasificación