玩转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多平台发布

相关推荐
岁忧2 分钟前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先
liliangcsdn40 分钟前
结合prompt分析NodeRAG的build过程
java·服务器·人工智能·数据分析·知识图谱
黑色的山岗在沉睡1 小时前
LeetCode 189. 轮转数组
java·算法·leetcode
会飞的小蛮猪1 小时前
Jenkins运维之路(权限分配&忘记admin密码)
java·运维·经验分享·jenkins·prometheus
slim~1 小时前
Java基础第9天总结(可变参数、Collections、斗地主)
java·开发语言
豆沙沙包?2 小时前
2025年- H118-Lc86. 分隔链表(链表)--Java版
java·数据结构·链表
A尘埃2 小时前
智能工单路由系统(Java)
java·开发语言·智能工单
失散133 小时前
分布式专题——1.1 Redis单机、主从、哨兵、集群部署
java·数据库·redis·分布式·架构
刘一说3 小时前
Linux调试命令速查:Java/微服务必备
java·linux·微服务