玩转springboot之springboot定制嵌入式的servlet

springboot定制嵌入式的servlet容器

修改容器配置

有两种方式可以修改容器的配置

  • 可以直接在配置文件中修改和server有关的配置

    server.port=8081
    server.tomcat.uri-encoding=UTF-8

    //通用的Servlet容器设置
    server.xxx
    //指定Tomcat的设置
    server.tomcat.xxx

  • 编写一个EmbeddedServletContainerCustomizer组件来进行嵌入式的Servlet容器的定制器,来修改Servlet容器的配置

    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
    return new EmbeddedServletContainerCustomizer() {

    复制代码
          //定制嵌入式的Servlet容器相关的规则
          @Override
          public void customize(ConfigurableEmbeddedServletContainer container) {
              container.setPort(8081);
          }
      };

    }

其实对于server的配置所采用的ServerProperties也是一个EmbeddedServletContainerCustomizer

复制代码
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
      implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {

替换为其他的servlet容器

默认springboot使用的是tomcat作为servlet容器,可以将servlet容器替换为jetty

复制代码
<!-- 排除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>

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

https://zhhll.icu/2021/框架/springboot/基础/6.定制嵌入式的servlet容器/

本文由mdnice多平台发布

相关推荐
daidaidaiyu33 分钟前
一文学习 工作流开发 BPMN、 Flowable
java
SuniaWang2 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
sheji34162 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
m0_726965982 小时前
面面面,面面(1)
java·开发语言
xuhaoyu_cpp_java3 小时前
过滤器与监听器学习
java·经验分享·笔记·学习
程序员小假3 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha4 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34164 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
木井巳4 小时前
【递归算法】子集
java·算法·leetcode·决策树·深度优先