Spring boot整合tomcat底层原理剖析

目录
  • 本文结论
  • spring-boot-starter-web内部有什么?
  • TomcatServletWebServerFactory的作用:获取WebServer对象
  • spring boot启动的时候启动tomcat
  • 获取tomcat的配置
  • ServletWebServerFactoryCustomizer这个Bean是哪里的?

从源码层面理解spring boot的默认web容器,以及他们是如何关联起来的。

本文结论

  • 源码基于spring boot2.6.6
  • 项目的pom.xml中存在spring-boot-starter-web的时候,在项目启动时候就会自动启动一个Tomcat。
  • 自动配置类ServletWebServerFactoryAutoConfiguration找到系统中的所有web容器。我们以tomcat为主。
  • 构建TomcatServletWebServerFactory的bean。
  • SpringBoot的启动过程中,会调用核心的refresh方法,内部会执行onRefresh()方法,onRefresh()方法是一个模板方法,他会执行会执行子类ServletWebServerApplicationContext的onRefresh()方法。
  • onRefresh()方法中调用getWebServer启动web容器。

spring-boot-starter-web内部有什么?

  • 在spring-boot-starter-web这个starter中,其实内部间接的引入了spring-boot-starter-tomcat这个starter,这个spring-boot-starter-tomcat又引入了tomcat-embed-core依赖,所以只要我们项目中依赖了spring-boot-starter-web就相当于依赖了Tomcat。

自动配置类:ServletWebServerFactoryAutoConfiguration在spring-boot-autoconfigure-2.6.6.jar这个包中的spring.factories文件内,配置了大量的自动配置类,其中就包括自动配置tomcat的自动配置类:ServletWebServerFactoryAutoConfiguration

自动配置类的代码如下

// full模式
@Configuration(proxyBeanMethods = false)

// 配置类解析顺序
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)

// 条件注解:表示项目依赖中要有ServletRequest类(server api)
@ConditionalOnClass(ServletRequest.class)
// 表示项目应用类型得是SpringMVC(在启动过程中获取的SpringBoot应用类型)
@ConditionalOnWebApplication(type = Type.SERVLET)

// 读取server下的配置文件
@EnableConfigurationProperties(ServerProperties.class)

// import具体的加载配置的类和具体web实现容器
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
		ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
		ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
		ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {
	......
}
  • ServletRequest是存在于tomcat-embed-core-9.0.60.jar中的的一个类,所以@ConditionalOnClass(ServletRequest.clas s)会满足。
  • spring-boot-starter-web中,间接的引入了spring-web、spring-webmvc等依赖,所以@ConditionalOnWebApplication(type = Type.SERVLET)条件满足。
  • 上面的俩个条件都满足,所以spring回去解析这个配置类,在解析过程中会发现他import了三个类!我们重点关注EmbeddedTomcat。其他俩个的内部条件注解不满足!
@Configuration(proxyBeanMethods = false)
// tomcat内部的类,肯定都存在
@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })

// 程序员如果自定义了ServletWebServerFactory的Bean,那么这个Bean就不加载。
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
static class EmbeddedTomcat {
    @Bean
    TomcatServletWebServerFactory tomcatServletWebServerFactory(
        ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
        ObjectProvider<TomcatContextCustomizer> contextCustomizers,
        ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
            TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
            // orderedStream()调用时会去Spring容器中找到TomcatConnectorCustomizer类型的Bean,默认是没有的,程序员可以自己定义。这个Bean可以设置一些tomcat的配置,比如端口、协议...
            // TomcatConnectorCustomizer:是用来配置Tomcat中的Connector组件的
            factory.getTomcatConnectorCustomizers().addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));
            // TomcatContextCustomizer:是用来配置Tomcat中的Context组件的
            factory.getTomcatContextCustomizers().addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));
            // TomcatProtocolHandlerCustomizer:是用来配置Tomcat中的ProtocolHandler组件的
            factory.getTomcatProtocolHandlerCustomizers().addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));
            return factory;
        }
    }
}
  • 对于另外的EmbeddedJetty和EmbeddedUndertow,逻辑类似,都是判断项目依赖中是否有Jetty和Undertow的依赖,如果有,那么对应在Spring容器中就会存在JettyServletWebServerFactory类型的Bean、或者存在UndertowServletWebServerFactory类型的Bean。

TomcatServletWebServerFactory的作用:获取WebServer对象

  • TomcatServletWebServerFactory他实现了ServletWebServerFactory这个接口。
  • ServletWebServerFactory接口内部只有一个方法是获取WebServer对象。

  • WebServer拥有启动、停止、获取端口等方法,就会发现WebServer其实指的就是Tomcat、Jetty、Undertow。

  • 而TomcatServletWebServerFactory就是用来生成Tomcat所对应的WebServer对象,具体一点就是TomcatWebServer对象,并且在生成TomcatWebServer对象时会把Tomcat给启动起来。
  • 在源码中,调用TomcatServletWebServerFactory对象的getWebServer()方法时就会启动Tomcat。
public WebServer getWebServer(ServletContextInitializer... initializers) {
    if (this.disableMBeanRegistry) {
        Registry.disableRegistry();
    }
    // 构建tomcat对象
    Tomcat tomcat = new Tomcat();

    // 设置相关的属性
    File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
    tomcat.setBaseDir(baseDir.getAbsolutePath());
    for (LifecycleListener listener : this.serverLifecycleListeners) {
        tomcat.getServer().addLifecycleListener(listener);
    }
    Connector connector = new Connector(this.protocol);
    connector.setThrowOnFailure(true);
    tomcat.getService().addConnector(connector);
    customizeConnector(connector);
    tomcat.setConnector(connector);
    tomcat.getHost().setAutoDeploy(false);
    configureEngine(tomcat.getEngine());
    for (Connector additionalConnector : this.additionalTomcatConnectors) {
        tomcat.getService().addConnector(additionalConnector);
    }
    prepareContext(tomcat.getHost(), initializers);

    // 启动tomcat,这个方法内部有this.tomcat.start();
    return getTomcatWebServer(tomcat);
}

spring boot启动的时候启动tomcat

  • SpringBoot的启动过程中,会调用核心的refresh方法,内部会执行onRefresh()方法,onRefresh()方法是一个模板方法,他会执行会执行子类ServletWebServerApplicationContext的onRefresh()方法。
protected void onRefresh() {
    // 模板方法,先调用它父类的,一般是空方法
    super.onRefresh();
    try {
        // 创建web容器
        createWebServer();
    }
    catch (Throwable ex) {
        throw new ApplicationContextException("Unable to start web server", ex);
    }
}

这个方法会调用createWebServer()方法。

// 最核心的俩行代码
private void createWebServer() {
    ......
    // 获取web容器,多个或者没有的时候报错
    ServletWebServerFactory factory = getWebServerFactory();

    // 调用这个容器的getWebServer方法,上面的启动tomcat的方法!
    this.webServer = factory.getWebServer(getSelfInitializer());
    ......
}
  • getWebServerFactory控制项目组有且只能有一个web容器!
protected ServletWebServerFactory getWebServerFactory() {
    // Use bean names so that we don't consider the hierarchy

    // 得到所有类型为ServletWebServerFactory的Bean。TomcatServletWebServerFactory、JettyServletWebServerFactory、UndertowServletWebServerFactory都是他得到子类!
    String[] beanNames = getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);

    // 不存在,报错
    if (beanNames.length == 0) {
        throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.");
    }

    // 存在不止一个,报错!
    if (beanNames.length > 1) {
        throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans : " + StringUtils.arrayToCommaDelimitedString(beanNames));
    }

    // 返回唯一的一个web容器!
    return getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
}

获取tomcat的配置

  • 自动配置类ServletWebServerFactoryAutoConfiguration上除了import三个web容器,还import了BeanPostProcessorsRegistrar。
  • BeanPostProcessorsRegistrar实现了ImportBeanDefinitionRegistrar,所以他会在spring启动的时候调用registerBeanDefinitions方法。
  • registerBeanDefinitions会注册一个Bean:webServerFactoryCustomizerBeanPostProcessor。
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    // Bean工厂,一个Aware回调进行赋值
    if (this.beanFactory == null) {
        return;
    }
    // 注册webServerFactoryCustomizerBeanPostProcessor这个Bean。
    registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor",
        WebServerFactoryCustomizerBeanPostProcessor.class,
        WebServerFactoryCustomizerBeanPostProcessor::new);

    // 注册errorPageRegistrarBeanPostProcessor
    registerSyntheticBeanIfMissing(registry, "errorPageRegistrarBeanPostProcessor",
        ErrorPageRegistrarBeanPostProcessor.class, ErrorPageRegistrarBeanPostProcessor::new);
}
  • webServerFactoryCustomizerBeanPostProcessor实现了BeanPostProcessor,所以他会在启动的时候调用postProcessBeforeInitialization方法。
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
    // 找到WebServerFactoryCustomizer的Bean
    LambdaSafe.callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory)
        // 标记日志用的类
        .withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
        // 调用customize方法,传入webServerFactory
        .invoke((customizer) -> customizer.customize(webServerFactory));
}
  • postProcessBeforeInitialization中会调用WebServerFactoryCustomizer类customize方法,在系统中的唯一实现:ServletWebServerFactoryCustomizer的customize方法。
  • customize把配置中的内容设置到ConfigurableServletWebServerFactory对象中。他的实现TomcatServletWebServerFactory在启动的时候就会有值!
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
    PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
    map.from(this.serverProperties::getPort).to(factory::setPort);
    map.from(this.serverProperties::getAddress).to(factory::setAddress);
    map.from(this.serverProperties.getServlet()::getContextPath).to(factory::setContextPath);
    map.from(this.serverProperties.getServlet()::getApplicationDisplayName).to(factory::setDisplayName);
    map.from(this.serverProperties.getServlet()::isRegisterDefaultServlet).to(factory::setRegisterDefaultServlet);
    map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession);
    map.from(this.serverProperties::getSsl).to(factory::setSsl);
    map.from(this.serverProperties.getServlet()::getJsp).to(factory::setJsp);
    map.from(this.serverProperties::getCompression).to(factory::setCompression);
    map.from(this.serverProperties::getHttp2).to(factory::setHttp2);
    map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader);
    map.from(this.serverProperties.getServlet()::getContextParameters).to(factory::setInitParameters);
    map.from(this.serverProperties.getShutdown()).to(factory::setShutdown);
    for (WebListenerRegistrar registrar : this.webListenerRegistrars) {
        registrar.register(factory);
    }
    if (!CollectionUtils.isEmpty(this.cookieSameSiteSuppliers)) {
        factory.setCookieSameSiteSuppliers(this.cookieSameSiteSuppliers);
    }
}

ServletWebServerFactoryCustomizer这个Bean是哪里的?

  • 在我们自动配置类ServletWebServerFactoryAutoConfiguration中定义。
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties, ObjectProvider<WebListenerRegistrar> webListenerRegistrars, ObjectProvider<CookieSameSiteSupplier> cookieSameSiteSuppliers) {
    return new ServletWebServerFactoryCustomizer(serverProperties,webListenerRegistrars.orderedStream().collect(Collectors.toList()),cookieSameSiteSuppliers.orderedStream().collect(Collectors.toList()));
}

到此这篇关于Spring boot整合tomcat底层原理的文章就介绍到这了,更多相关Spring boot整合tomcat内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Springboot内嵌tomcat应用原理深入分析

    目录 默认Servlet容器 切换Servlet容器 内嵌tomcat自动配置原理 tomcat自动配置类 tomcat工厂类 何时被调用 onRefresh() finishRefresh() springboot版本:2.2.9.RELEASE. 默认Servlet容器 springboot默认支持tomcat.jetty.undertow作为底层容器, 一旦引入spring-boot-starter-web模块,就默认使用tomcat. <dependency> <groupId&

  • 大厂禁止SpringBoot在项目使用Tomcat容器原理解析

    目录 前言 SpringBoot中的Tomcat容器 SpringBoot设置Undertow Tomcat与Undertow的优劣对比 最后 前言 在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat.同时,SpringBoot也支持Undertow容器,我们可以很方便的用Undertow替换Tomcat,而Undertow的性能和内存使用方面都优于Tomcat,那我们如何使用Undertow技术呢?本文将为大家细细讲解

  • 浅谈SpringBoot内嵌Tomcat的实现原理解析

    一.序言 使用SpringBoot经常会使用内嵌的tomcat做为项目的启动容器,本文将从源码的角度出发,剖析SpringBoot内嵌Tomcat的实现原理,讨论Tomcat何时创建.何时启动以及怎么启动. 二.引入Tomcat组件 导入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId&

  • SpringBoot内置tomcat启动原理详解

    前言 不得不说SpringBoot的开发者是在为大众程序猿谋福利,把大家都惯成了懒汉,xml不配置了,连tomcat也懒的配置了,典型的一键启动系统,那么tomcat在springboot是怎么启动的呢? 内置tomcat 开发阶段对我们来说使用内置的tomcat是非常够用了,当然也可以使用jetty. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-bo

  • Spring boot整合tomcat底层原理剖析

    目录 本文结论 spring-boot-starter-web内部有什么? TomcatServletWebServerFactory的作用:获取WebServer对象 spring boot启动的时候启动tomcat 获取tomcat的配置 ServletWebServerFactoryCustomizer这个Bean是哪里的? 从源码层面理解spring boot的默认web容器,以及他们是如何关联起来的. 本文结论 源码基于spring boot2.6.6 项目的pom.xml中存在spr

  • 详解spring boot整合JMS(ActiveMQ实现)

    本文介绍了spring boot整合JMS(ActiveMQ实现),分享给大家,也给自己留个学习笔记. 一.安装ActiveMQ 具体的安装步骤,请参考我的另一篇文章:http://www.jb51.net/article/127117.htm 二.新建spring boot工程,并加入JMS(ActiveMQ)依赖 三.工程结构 pom依赖如下: <?xml version="1.0" encoding="UTF-8"?> <project xm

  • Spring Boot 整合mybatis 与 swagger2

    之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到了spring boot 后发现搭建一个web项目真的是1分钟的事情,再也不用去管那么多xml配置,简直神清气爽,不用看那么多一坨xml,下面是我把以前的一些ssm项目改成了spring boot + mybatis,相对于来说优点太明显了 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自

  • 移动开发Spring Boot外置tomcat教程及解决方法

    springboot微服务内置了tomcat,在工程目录下执行:mvn clean package,可以将项目打成jar,通过java -jar jar包名.jar启动项目. 有哪些场景需要将springboot打成war包去部署呢? 1.一个tomcat管理多个项目 2.springboot整合jsp等 解决方法: 1.<packaging>jar</packaging>中的jar改成war 2.引入依赖: <dependency> <groupid>or

  • spring boot jar的启动原理解析

     1.前言 近来有空对公司的open api平台进行了些优化,然后在打出jar包的时候,突然想到以前都是对spring boot使用很熟练,但是从来都不知道spring boot打出的jar的启动原理,然后这回将jar解开了看了下,与想象中确实大不一样,以下就是对解压出来的jar的完整分析. 2.jar的结构 spring boot的应用程序就不贴出来了,一个较简单的demo打出的结构都是类似,另外我采用的spring boot的版本为1.4.1.RELEASE网上有另外一篇文章对spring

  • Spring Boot 整合单机websocket的步骤 附github源码

    websocket 概念 websocket 是一个通信协议,通过单个 TCP 连接提供全双工通信.websocket 连接成功后,服务端和客户可以进行双向通信.不同于 http 通信协议需要每次由客户端发起,服务响应到客户端. websocket 相对轮询也能节约带宽,并且能实时的进行通信. 整合步骤 1. 添加 maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact

  • Spring Boot 整合 Fisco Bcos的案例分析(区块链)

    目录 简介 本地环境 主要流程: 1.Fisco Bcos环境搭建与验证 1.1.搭建单群组4节点联盟链: 1.2.检查证书 1.3.使用证书验证节点正确性 2.创建SpringBoot工程并配置依赖 2.1.创建SpringBoot工程: 2.2.配置pom.xml 3.2.配置节点证书: 3.3.编写controller 3.4.DemoBcosApplication默认不做修改 3.5.application.properties什么也没配置 4.生成jar包.部署服务器验证 4.1.本地

  • Spring boot整合Springfox生成restful的在线api文档

    目录 Springfox是什么,有什么用? Springfox的依赖 Springfox的配置 测试的Controll Springfox是什么,有什么用? Springfox基于Swagger,能更方便的集成到spring boot 中,Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.Swagger的目标是对REST API定义一个标准的和语言无关的接口,可让人和计算机无需访问源码.文档或网络流量监测就可以发现和理解服务的能力.当通过

  • Spring boot整合jsp和tiles模板示例

    目录 首先贴上我的pox.xml文件,有详细的支持注释说明 <strong><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation

  • Spring boot整合jsp和tiles模板示例

    首先贴上我的pox.xml文件,有详细的支持注释说明 <strong><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&q

随机推荐