一:springboot内嵌servlet切换方式如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 这里排除掉tomcat容器 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Jetty适合长连接应用,就是聊天类的长连接 -->
<!-- 使用Jetty,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
二:切换原理
1,ServletWebServerFactoryAutoConfiguration 引入了
EmbeddedTomcat.class,
EmbeddedJetty.class,
EmbeddedUndertow.class三个类
@Configuration(
proxyBeanMethods = false
)
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
EmbeddedTomcat.class,
EmbeddedJetty.class,
EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
2,在这EmbeddedTomcat.class,
EmbeddedJetty.class,
EmbeddedUndertow.class三个类中由于使用了@ConditionalOnClass指定需要依赖的包完成注入
如EmbeddedTomcat.class
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
@ConditionalOnMissingBean(
value = {ServletWebServerFactory.class},
search = SearchStrategy.CURRENT
)
static class EmbeddedTomcat {
本文介绍了如何在SpringBoot项目中将默认的Tomcat内嵌容器切换为Jetty。通过排除spring-boot-starter-tomcat依赖并引入spring-boot-starter-jetty,可以实现容器的更换。此外,解释了ServletWebServerFactoryAutoConfiguration配置类在其中的作用,以及EmbeddedTomcat、EmbeddedJetty等类的条件注解用于决定哪个内嵌服务器启动。
825

被折叠的 条评论
为什么被折叠?



