前言
默認情況下,Spring Boot會使用內置的tomcat容器去運行應用程序,但偶爾我們也會考慮使用Jetty去替代Tomcat;
對於Tomcat和Jetty,Spring Boot分別提供了對應的starter,以便盡可能的簡化我們的開發過程;
當我們想使用Jetty的時候,可以參考以下步驟來使用。
添加spring-boot-starter-jetty依賴
我們需要更新pom.xml
文件,添加spring-boot-starter-jetty
依賴,同時我們需要排除spring-boot-starter-web
默認的spring-boot-starter-tomcat
依賴,如下所示:
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
如果我們的工程是使用Gradle構建的話,可以使用以下方式達到同樣的效果:
configurations { compile.exclude module: "spring-boot-starter-tomcat" } dependencies { compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT") compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT") }
重啟完成之后,再次查看控制台打印的日志信息,Jetty started,以此可以確定替換jetty成功