Home > Java > javaTutorial > How to switch SpringBoot to other embedded Servlet containers

How to switch SpringBoot to other embedded Servlet containers

王林
Release: 2023-05-13 22:58:12
forward
1521 people have browsed it

How to switch to other embedded Servlet containers (Jetty and Undertow)

The built-in Servlet container used by SpringBoot by default is Tomcat

However, SpringBoot also supports other Servlet containers. The default Tomcat is just One of them

SpringBoot also supports Jetty and Undertow

  • JettySuitable for long link applications such as chat

  • Undertow is a high-performance non-blocking Servlet container with very good concurrency performance but does not support jsp

How to switch SpringBoot to other embedded Servlet containers

If To switch, you only need toExclude the built-in Tomcat starter in the pom file and then introduce the starters of other Servlet containers

spring-boot-starter-web introduces spring-boot -starter-tomcat Therefore, Tomcat is used by default

So just exclude the original Tomcat and then introduce the dependencies of the Servlet container to be added:

Switch to Jetty:

<!-- 引入Web模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除tomcat容器 -->
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>
Copy after login

How to switch SpringBoot to other embedded Servlet containers

Jetty started successfully

Similarly switch to undertow:

<!-- 引入Web模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除tomcat容器 -->
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 引入其它的Servlet容器 -->
<dependency>
    <artifactId>spring-boot-starter-undertow</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>
Copy after login

How to switch SpringBoot to other embedded Servlet containers

Undertow started successfully

SpringBoot web development_embedded Servlet container

1. Switch the embedded Servlet container

1.1.SpringBoot Supported Servlet types and switching

1) Default supported webServers: Tomcat, Jrtty, Undertow

2) Switching servers

How to switch SpringBoot to other embedded Servlet containers

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Copy after login

1.2. Principle

1) When the SpringBoot application starts, it is found that it is currently a Web application. web scenario package - import tomcat

2) The web application will create a web version of the ioc container ServletWebServerApplicationContext

3) ServletWebServerApplicationContext When starting, look for ServletWebServerFactory (Servlet's web server factory---> Servlet web server)

4) SpringBoot has many WebServer factories by default at the bottom layer;

  • TomcatServletWebServerFactory

  • JettyServletWebServerFactory

  • UndertowServletWebServerFactory

5) There will be an automatic configuration directly at the bottom layer kind.

ServletWebServerFactoryAutoConfiguration imports ServletWebServerFactoryConfiguration (configuration class)

6) ServletWebServerFactoryConfiguration configuration class determines which web server package has been imported into the system based on dynamics. (The default is that web-starter imports the tomcat package), and there is TomcatServletWebServerFactory

7 in the container) TomcatServletWebServerFactory creates a Tomcat server and starts it; the constructor of TomcatWebServer has an initialization method initialize---this.tomcat.start() ;

8) Embedded server means manually calling the code to start the server (tomcat core jar package exists)

2. Customized Servlet container

2.1. Modify the configuration file

Customize the Servlet container by setting the application.properties configuration file

#修改servlet容器
server.port=8000
#server.undertow.accesslog.dir=/tmp
Copy after login

2.2, implement WebServerFactoryCustomizer

xxxxxCustomizer: Customizer, you can change the default rules of xxxx

Modify the response port number of the Servlet container through WebServerFactoryCustomizer

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
 
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
 
    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }
}
Copy after login

2.3, ConfigurableServletWebServerFactory

Directly customize the interface implementation class of ConfigurableServletWebServerFactory

public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry {
    void setPort(int port); 
    void setAddress(InetAddress address); 
    void setErrorPages(Set<? extends ErrorPage> errorPages); 
    void setSsl(Ssl ssl); 
    void setSslStoreProvider(SslStoreProvider sslStoreProvider); 
    void setHttp2(Http2 http2); 
    void setCompression(Compression compression); 
    void setServerHeader(String serverHeader); 
    default void setShutdown(Shutdown shutdown) {
    }
}
Copy after login

ConfigurableServletWebServerFactory Interface implementation class (default implementation class in the framework):

How to switch SpringBoot to other embedded Servlet containers

By customizing the implementation class of the ConfigurableServletWebServerFactory interface, you can customize the Servlet container

The above is the detailed content of How to switch SpringBoot to other embedded Servlet containers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template