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

相关推荐
一只会写代码的猫3 小时前
面向高性能计算与网络服务的C++微内核架构设计与多线程优化实践探索与经验分享
java·开发语言·jvm
萤丰信息4 小时前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧4 小时前
Eclipse为方法添加注释
java·ide·eclipse
我叫张小白。5 小时前
Spring Boot拦截器详解:实现统一的JWT认证
java·spring boot·web·jwt·拦截器·interceptor
Gerardisite7 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
闲人编程7 小时前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器
故渊ZY8 小时前
Java 代理模式:从原理到实战的全方位解析
java·开发语言·架构
匿者 衍8 小时前
POI读取 excel 嵌入式图片(支持wps 和 office)
java·excel
一个尚在学习的计算机小白8 小时前
java集合
java·开发语言
IUGEI8 小时前
synchronized的工作机制是怎样的?深入解析synchronized底层原理
java·开发语言·后端·c#