Home >Java >javaTutorial >How to switch SpringBoot to other embedded Servlet containers
However, SpringBoot also supports other Servlet containers. The default Tomcat is just One of them
Jetty
Suitable 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
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>
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>
Undertow started successfully
1.1.SpringBoot Supported Servlet types and switching
1) Default supported webServers: Tomcat, Jrtty, Undertow
2) Switching servers
<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>
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.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
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); } }
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) { } }
ConfigurableServletWebServerFactory Interface implementation class (default implementation class in the framework):
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!