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

相关推荐
计算机学姐16 分钟前
基于SpringBoot的咖啡店管理系统【个性化推荐+数据可视化统计+配送信息】
java·vue.js·spring boot·后端·mysql·信息可视化·tomcat
My的梦想已实现26 分钟前
关于JAVA Springboot集成支付后打包JAR之后报安全错误的处理
java·spring boot·jar
ooseabiscuit41 分钟前
SpringBoot3整合FastJSON2如何配置configureMessageConverters
java
ok_hahaha1 小时前
java从头开始-黑马点评-Redission
java·开发语言
无巧不成书02181 小时前
Java面向对象零基础实战:从Employee类吃透自定义类核心,掌握封装精髓
java·开发语言·java入门·面向对象·自定义类·employee类·java核心技术
小江的记录本1 小时前
【注解】常见 Java 注解系统性知识体系总结(附《全方位对比表》+ 思维导图)
java·前端·spring boot·后端·spring·mybatis·web
跃上青空1 小时前
Java如何优雅的使用fastjson2进行枚举序列化/反序列化,欢迎探讨
java·开发语言
Mr.45671 小时前
Spring Boot 集成 PostgreSQL 表级备份与恢复实战
java·spring boot·后端·postgresql
架构师沉默2 小时前
为什么一个视频能让全国人民同时秒开?
java·后端·架构
生命不息战斗不止(王子晗)2 小时前
mysql基础语法面试题
java·数据库·mysql