崛起于Springboot2.0.X之切换使用Servlet容器Jetty、Tomcat、Undertow(38)

一、配置

1.1 pom配置

<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>

若是使用Jetty容器,那么添加web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

若是使用Undertow容器,那么添加spring

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency

二、切换Servlet容器

2.1 版本2.0如下,1.X的版本切换Jetty、Undertow

在启动类,添加tomcat

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    JettyEmbeddedServletContainerFactory factory =
            new JettyEmbeddedServletContainerFactory();
    return factory;
}

若是你想换成Undertow,那么把上面中Jetty五个字母替换成Undertowspring-boot

2.2 版本2.0以上,2.X版本切换Jetty、Undertow

在启动类添加spa

@Bean public ServletWebServerFactory servletContainer() {
    JettyServletWebServerFactory tomcat = new JettyServletWebServerFactory();
    return tomcat;
}

若是你想换成Undertow,那么把上面中Jetty五个字母替换成Undertowservlet

相关文章
相关标签/搜索