SpringBoot之——内置web容器切换(Jetty、Netty、Tomcat、Undertow)默认:Tomcat
目录查看内置web容器切换至Tomcat容器切换至Jetty容器切换至Netty容器切换至Undertow容器链接:[SpringBoot内置web容器切换 源代码下载地址](https://download.csdn.net/download/JAVA_MHH/16391704)查看内置web容器spring-boot-autoconfigure-2.3.2.RELEASE.jar 找到下面的e
目录
查看内置web容器
spring-boot-autoconfigure-2.3.2.RELEASE.jar 里找到下面的embedded包,可以看到以下4种容器:Jetty,Netty,Tomcat,Undertow
切换至Tomcat容器
1.首先引入pom依赖
我们可以从依赖项中看到SpringBooot依赖了tomcat容器,所以启动web时就是启动的tomcat
<!--引入springboot父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<!--引入启动器依赖 里面就有默认的tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.启动类测试
结果: 可以看到
Tomcat started on port(s): 8080
,说明现在用的内置容器就是tomcat
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwitchBuiltInServersApplication {
public static void main(String[] args) {
SpringApplication.run(SwitchBuiltInServersApplication.class,args);
}
}
切换至Jetty容器
1.首先引入pom依赖
注意:Maven配置阿里云的settings,如果本地没有Jetty的jar包,Maven会自动去阿里云仓库拉取相关jar包
因为web里引入的是tomcat容器,所以这里
首先排除tomcat容器
,然后引入Jetty容器
<!--引入springboot父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<!--引入启动器依赖 里面就有默认的tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入Jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
2.启动类测试
结果: 可以看到
Jetty started on port(s) 8080
,说明现在用的内置容器就是Jetty
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwitchBuiltInServersApplication {
public static void main(String[] args) {
SpringApplication.run(SwitchBuiltInServersApplication.class,args);
}
}
切换至Netty容器
1.首先引入pom依赖
注意:Maven配置阿里云的settings,如果本地没有Netty的jar包,Maven会自动去阿里云仓库拉取相关jar包
因为web里引入的是tomcat容器,所以这里
首先排除tomcat容器
,然后引入Netty容器
<!--引入springboot父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<!--引入启动器依赖 里面就有默认的tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入Netty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
2.启动类测试
结果: 可以看到
Netty started on port(s): 8080
,说明现在用的内置容器就是Netty
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwitchBuiltInServersApplication {
public static void main(String[] args) {
SpringApplication.run(SwitchBuiltInServersApplication.class,args);
}
}
切换至Undertow容器
1.首先引入pom依赖
注意:Maven配置阿里云的settings,如果本地没有Netty的jar包,Maven会自动去阿里云仓库拉取相关jar包
因为web里引入的是tomcat容器,所以这里
首先排除tomcat容器
,然后引入Undertow容器
<!--引入springboot父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<!--引入启动器依赖 里面就有默认的tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入Undertow-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
2.启动类测试
结果: 可以看到
Undertowstarted on port(s): 8080
,说明现在用的内置容器就是Undertow
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwitchBuiltInServersApplication {
public static void main(String[] args) {
SpringApplication.run(SwitchBuiltInServersApplication.class,args);
}
}
链接:SpringBoot内置web容器切换 源代码下载地址
更多推荐
所有评论(0)