玩转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 分钟前
MQTT从入门到实战
java·后端·kafka·消息队列·嵌入式
福大大架构师每日一题6 分钟前
milvus v2.6.8 发布:搜索高亮上线,性能与稳定性全面跃升,生产环境强烈推荐升级
android·java·milvus
键盘林7 分钟前
java: 找不到符号
java
、BeYourself15 分钟前
项目案例-构建 AI 驱动的文档搜索系统-2
java·人工智能·springai·项目案例
淺川之夏19 分钟前
abstract 类,里面引用@Autowired ,使用注入类的方法,报空指针异常
java·开发语言
疯狂成瘾者23 分钟前
后端Spring Boot 核心知识点
java·spring boot·后端
IT 行者1 小时前
Spring Boot 4.x 安全监控新篇章:基于 ObservationFilterChainDecorator 的可观测性实践
java·spring boot·后端
pyniu1 小时前
Spring Boot租房管理系统
java·spring boot·后端
野生技术架构师1 小时前
TokenRetryHelper 详解与 Spring Boot 迁移方案
java·spring boot·后端
蚰蜒螟1 小时前
Redis网络层深度解析:数据如何写回客户端
java·开发语言·bootstrap