springboot将tomcat换为jetty
springboot默认加载的是tomcat容器,它还有另外两个可选择的容器:jetty和undertow,还有一个Configurable是可以自定义的一个接口;本文不讨论tomcat,jetty,undertow的优劣。感兴趣的同学可以自己查询。
通过org.springframework.boot.web.servlet.server.ServletWebServerFactory的实现就可以知道;那怎么样将默认的tomcat换成jetty或者undertow呢?
其实很简单,以jetty为例,只需要引入org.springframework.boot:spring-boot-starter-jetty包并且排除spring-boot-starter-tomcat包即可;若是要使用undertow,则引用包org.springframework.boot:spring-boot-starter-undertow
gradle配置如下:
configurations { compile.exclude module: "spring-boot-starter-logging" compile.exclude module: "spring-boot-starter-tomcat" } dependencies { compile("com.alibaba:fastjson:1.2.75") compile("mysql:mysql-connector-java") //spring compile("org.springframework.boot:spring-boot-starter-jetty") compile("org.springframework.boot:spring-boot-starter") compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4") compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-actuator") //compile("org.springframework.boot:spring-boot-starter-aop") //compile("org.springframework.boot:spring-boot-starter-log4j2") //lombok compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' //log compile "org.apache.logging.log4j:log4j-api:2.14.0" compile "org.apache.logging.log4j:log4j-core:2.14.0" //test testCompile('org.springframework.boot:spring-boot-starter-test') testImplementation 'io.projectreactor:reactor-test' }
总结:springboot换容器,只需要引入相关包,并且排除默认的tomcat包即可