SpringBoot-Web开发之嵌入式容器

切换嵌入式Servlet容器

  1. 默认支持的web服务器
  • Tomcat
  • Jetty
  • Undertow
  1. 切换服务器
  • 排除默认tomcat
XML 复制代码
<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>
  • 引入其他服务器
XML 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

Servlet容器配置原理

  1. SpringBoot应用启动时,发现当前是Web应用
  • spring-boot-starter-web默认导入了tomcat
  1. web应用会创建一个web版的ioc容器
  • 启动的时候,ServletWebServerApplicationContext 寻找 ServletWebServerFactory
  • 也就是Servlet 的web服务器工厂 找 Servlet 的web服务器
  1. web服务器的自动配置类ServletWebServerFactoryAutoConfiguration
  • 导入了ServletWebServerFactoryConfiguration(配置类)
  1. ServletWebServerFactoryConfiguration配置类
  • 动态判断系统中到底导入了那个Web服务器的包
  • web-starter默认是导入tomcat包,容器中就有 TomcatServletWebServerFactory
  1. 获取到唯一确定的服务器factory,调用getWebServer()
  • 创建出Tomcat服务器
  • TomcatWebServer 的构造器进行初始化initialize()
  • 手动调用this.tomcat.start(),启动服务器

修改Servlet容器

  1. 配置文件中修改默认配置
  • server.xxx
  • 配置文件的值和ServletWebServerFactory是绑定的
  1. 自定义 ConfigurableServletWebServerFactory
  • 一般直接使用配置文件配置即可
java 复制代码
@Component
public class CustomizationBean implements 
        WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }

}

定制化的常见方式

  1. 修改配置文件

  2. xxxxxCustomizer

  3. 编写自定义的配置类 xxxConfiguration

  4. Web应用编写一个配置类实现 WebMvcConfigurer

  • 如果配置类加@EnableWebMvc,全面接管SpringMVC,所有规则都需要自己重新配置
  • 慎用注解@EnableWebMvc
java 复制代码
/**
 * @EnableWebMvc:全面接管
 *      1、静态资源?视图解析器?欢迎页.....全部失效
 */
@EnableWebMvc
@Configuration
public class AdminWebConfig implements WebMvcConfigurer{

    /**
     * 定义静态资源行为
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 访问  /aa/** 所有请求都去 classpath:/static/ 下面进行匹配
         */
        registry.addResourceHandler("/aa/**")
                .addResourceLocations("classpath:/static/");
    }
}

@EnableWebMvc原理

  • 一旦使用 @EnableWebMvc 、会 @Import(DelegatingWebMvcConfiguration.class)
  • DelegatingWebMvcConfiguration 的作用,只保证SpringMVC最基本的使用,然后搭配系统中所有的 WebMvcConfigurer
  • WebMvcAutoConfiguration因此失效
相关推荐
荔枝hu11 分钟前
springboot和shiro组合引入SseEmitter的一些坑
java·spring boot·后端·sseeitter
老华带你飞29 分钟前
健身房|基于springboot + vue健身房管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
JIngJaneIL34 分钟前
基于Java酒店预约系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
曹牧1 小时前
Java:List<Map<String, String>>转换为字符串
java·开发语言·windows
Unstoppable222 小时前
代码随想录算法训练营第 56 天 | 拓扑排序精讲、Dijkstra(朴素版)精讲
java·数据结构·算法·
qinyia2 小时前
WisdomSSH解决docker run命令中log-opt参数不支持导致的容器创建失败问题
java·docker·eureka
电饭叔2 小时前
不含Luhn算法《python语言程序设计》2018版--第8章14题利用字符串输入作为一个信用卡号之二(识别卡号有效)
java·python·算法
小付爱coding2 小时前
Claude Code安装教程【windows版本】
java·git·python
**蓝桉**3 小时前
数组的执行原理,java程序的执行原理
java·开发语言
YDS8293 小时前
MyBatis-Plus精讲 —— 从快速入门到项目实战
java·后端·spring·mybatis·mybatis-plus