当前位置: 首页 > news >正文

临朐网站建设建站首选哪家公司百度快速排名点击器

临朐网站建设建站首选哪家公司,百度快速排名点击器,商城网站前置审批,如果网站设计时SpringBoot 源码深度解析 第八章 内嵌Tomcat原理 文章目录SpringBoot 源码深度解析前言内嵌Tomcat自动配置原理SpringBoot启动内置tomcat流程getWebServer()的调用前言 内嵌Tomcat原理 Spring Boot默认支持Tomcat,Jetty,和Undertow作为底层容器。 而Sp…

SpringBoot 源码深度解析

第八章 内嵌Tomcat原理

文章目录

  • SpringBoot 源码深度解析
  • 前言
  • 内嵌Tomcat自动配置原理
  • SpringBoot启动内置tomcat流程
  • getWebServer()的调用


前言

内嵌Tomcat原理


Spring Boot默认支持Tomcat,Jetty,和Undertow作为底层容器。
Spring Boot默认使用Tomcat,一旦引入spring-boot-starter-web模块,就默认使用Tomcat容器。

内嵌Tomcat自动配置原理

那些看似简单的事物,其实并不简单。我们之所以觉得他简单,是因为复杂性都被隐藏了,大概率可以提出以下几个疑问

  1. SpringBoot是如何启动内置tomcat
  2. SpringBoot为什么可以响应请求,他是如何配置的SpringMvc

SpringBoot启动内置tomcat流程

进入SpringBoot启动类,点进@SpringBootApplication源码
在这里插入图片描述
在这里插入图片描述
继续点进@EnableAutoConfiguration进入该注解
在这里插入图片描述
@AutoConfigurationPackage主要实现自动配置包,会扫描@SpringbootApplication标注的类所在包名及其子包,将创建的组件添加到容器中;而@Import则是导入了AutoConfigurationImportSelector.class,实现查找classpath上所有jar包中的ETA-INF/spring.factories,找出其中的自动配置类并导入到容器中,其中Web容器所对应的自动配置类为ServletWebServerFactoryAutoConfiguration
在这里插入图片描述
这个spring.factories配置文件是加载的spring-boot-autoconfigure的配置文件
在这里插入图片描述
继续打开spring.factories配置文件,找到tomcat所在的类,tomcat加载在ServletWebServerFactoryAutoConfiguration配置类中
在这里插入图片描述
ServletWebServerFactoryAutoConfiguration中支持好几种web容器,比如TomcatJettyUndertow
在这里插入图片描述
对于EmbeddedTomcat,它本身是一个就是一个FactoryBean,是用来实例化TomcatServletWebServerFactory的。此时TomcatServletWebServerFactory中就包含了创建和启动Tomcat的方法getWebServer()
我们点击进入EmbeddedTomcat中,查看下的getWebServer()方法:
在这里插入图片描述
这段代码的作用就是处理Tomcat容器对象的创建、环境配置和启动。
继续进入getTomcatWebServer()等方法,一直往下跟到tomcat初始化方法,调用tomcat.start()方法,tomcat就正式开启运行:
在这里插入图片描述

getWebServer()的调用

首先进入SpringBoot启动类的run方法:

SpringApplication.run(HppaApplication.class, args);

然后进入run方法:

public ConfigurableApplicationContext run(String... args) {//StopWatch主要是用来统计每项任务执行时长,例如Spring Boot启动占用总时长。StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();configureHeadlessProperty();//第一步:获取并启动监听器 通过加载META-INF/spring.factories 完成了SpringApplicationRunListener实例化工作SpringApplicationRunListeners listeners = getRunListeners(args);//实际上是调用了EventPublishingRunListener类的starting()方法listeners.starting();
try {ApplicationArguments applicationArguments = newDefaultApplicationArguments(args);//第二步:构造容器环境,简而言之就是加载系统变量,环境变量,配置文件ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);//设置需要忽略的beanconfigureIgnoreBeanInfo(environment);//打印bannerBanner printedBanner = printBanner(environment);//第三步:创建容器context = createApplicationContext();//第四步:实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class },context);//第五步:准备容器 这一步主要是在容器刷新之前的准备动作。包含一个非常关键的操作:将启动类注入容器,为后续开启自动化配置奠定基础。prepareContext(context, environment, listeners,applicationArguments, printedBanner);//第六步:刷新容器 springBoot相关的处理工作已经结束,接下的工作就交给了spring。 内部会调用spring的refresh方法,// refresh方法在spring整个源码体系中举足轻重,是实现 ioc 和 aop的关键。refreshContext(context);//第七步:刷新容器后的扩展接口 设计模式中的模板方法,默认为空实现。如果有自定义需求,可以重写该方法。比如打印一些启动结束log,或者一些其它后置处理。afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(),stopWatch);}//发布应用已经启动的事件listeners.started(context);/** 遍历所有注册的ApplicationRunner和CommandLineRunner,并执行其run()方法。* 我们可以实现自己的ApplicationRunner或者CommandLineRunner,来对SpringBoot的启动过程进行扩展。*/callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, listeners);throw new IllegalStateException(ex);}try {//应用已经启动完成的监听事件listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, null);throw new IllegalStateException(ex);}return context;
}

其中refreshContext(context)主要负责容器的刷新;点击进入:

public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh();// Tell the subclass to refresh the internal bean factory.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.onRefresh();// Check for listener beans and register them.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.finishRefresh();}catch (BeansException ex) {if (logger.isWarnEnabled()) {logger.warn("Exception encountered during contextinitialization - " + "cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.destroyBeans();// Reset 'active' flag.cancelRefresh(ex);// Propagate exception to caller.throw ex;} finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...resetCommonCaches();}
}

一直点击refresh()方法``onRefresh()会调用到ServletWebServerApplicationContext中的createWebServer()
在这里插入图片描述

private void createWebServer() {WebServer webServer = this.webServer;ServletContext servletContext = getServletContext();if (webServer == null && servletContext == null) {ServletWebServerFactory factory = getWebServerFactory();this.webServer = factory.getWebServer(getSelfInitializer());} else if (servletContext != null) {try {getSelfInitializer().onStartup(servletContext);} catch (ServletException ex) {throw new ApplicationContextException("Cannot initialize servlet context", ex);}}initPropertySources();
}

createWebServer()就是启动web服务,但是还没有真正启动Tomcat,既然webServer是通过ServletWebServerFactory来获取的,那就来看看这个工厂。
在这里插入图片描述
点击进入getWebServer(ServletContextInitializer… initializers).方法
在这里插入图片描述

http://www.lbrq.cn/news/2350657.html

相关文章:

  • api网站制作网站排名优化师
  • 手机端网站建设教程黄页88网官网
  • 做虾皮网站制作网站需要什么软件
  • 进入官方网站淘宝怎么优化关键词步骤
  • 网站用什么做竞价托管怎么做
  • 重庆有没有做网站的税收大数据
  • 做网站 中企动力国际域名注册网站
  • h5平台网站开发网上推广app
  • 合肥学做网站app的学校seo有哪些经典的案例
  • 文章网站模板哪个好百度指数的功能
  • 手机网站怎么放到桌面上公司seo是指什么意思
  • 网站开发和后台维护关键词排名软件官网
  • 黄岛网站制作seo关键词优化公司
  • logo设计免费平台网站优化排名方案
  • 北京十大室内设计工作室如何seo推广
  • 做网站销售经常遇到的问题seo搜索引擎优化期末及答案
  • 在哪里个网站找专业做ps的人舟山百度seo
  • 网站的底部导航怎么做个人网页制作完整教程
  • 怎么看一个网站什么程序做的个人网页制作完整教程
  • 做网站需要准备什么搜索引擎优化seo专员招聘
  • 想采集某类型网站怎么做谷歌浏览器网页版
  • 动易门户网站价格手机管家一键优化
  • 网站网页和网址的关系百度关键词下拉有什么软件
  • dw建立网站之后怎么做东莞seo网站排名优化
  • 网站开发技术语言的选择系统优化是什么意思
  • 做网站用什么今天的新闻
  • 在百度建免费网站吗网络营销师证书含金量
  • 网站怎样做优化百度快照投诉中心官网
  • 网站html地图导航代码2021最近最火的关键词
  • 用mvc做网站的框架今日小说百度搜索风云榜
  • alpineLinux修改包管理为国内源
  • 【Linux】基本指令详解(二) 输入\输出重定向、一切皆文件、认识管道、man、cp、mv、echo、cat
  • 面试问题:
  • 经典排序算法之希尔排序
  • 分布式通信框架 - JGroups
  • GitCode 使用高频问题及解决方案