SpringBoot - 嵌入式容器配置与修改

【1】嵌入式容器

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器。

查看pom文件依赖图如下:

这里写图片描述


【2】配置嵌入式Tomcat容器

① 配置文件中进行配置

这里写图片描述


查看Server配置类ServerProperties,其属性和内部类如下:

这里写图片描述

即,与Server有关的配置与该类对应,该类中除了Tomcat还有jetty、undertow等容器可以进行配置。

如下图,在配置文件中对Tomcat进行配置:

这里写图片描述


② 编写一个EmbeddedServletContainerCustomizer

嵌入式的Servlet容器的定制器;来修改Servlet容器的配置。

示例如下:

    //配置嵌入式的Servlet容器
    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
            //定制嵌入式的Servlet容器相关的规则
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(8083);
            }
        };
    }

【3】切换其他容器

SpringBoot默认还支持Jetty和Undertow容器,Undertow是一个高性能非阻塞的Servlet容器,并发性能很好,但是不支持JSP。Jetty更适合开发一些长连接Web应用,如Web聊天。

如下图所示,是ConfigurableEmbeddedServletContainer继承树:

这里写图片描述

即,该container可以配置Tomcat、Jetty和UndertowServlet容器。

SpringBoot默认使用的是Tomcat,如果要切换其他容器,则只需要在pom文件中去掉Tomcat依赖,添加其他依赖,如Jetty。

示例如下:

<!-- 引入web模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>

</dependency>

<!--引入其他的Servlet容器-->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

猜你喜欢

转载自blog.csdn.net/j080624/article/details/80758527