博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot--启动流程、原理
阅读量:2442 次
发布时间:2019-05-10

本文共 2881 字,大约阅读时间需要 9 分钟。

SpringBoot启动流程

1、创建SpringApplication对象

initialize(sources);private void initialize(Object[] sources) {
//保存主配置类 if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources)); } //判断当前是否一个web应用 this.webEnvironment = deduceWebEnvironment(); //从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来 setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); //从类路径下找到ETA-INF/spring.factories配置的所有ApplicationListener setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); //从多个配置类中找到有main方法的主配置类 this.mainApplicationClass = deduceMainApplicationClass();}

在这里插入图片描述

在这里插入图片描述

运行run方法

public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHeadlessProperty(); //获取SpringApplicationRunListeners;从类路径下META-INF/spring.factories SpringApplicationRunListeners listeners = getRunListeners(args); //回调所有的获取SpringApplicationRunListener.starting()方法 listeners.starting(); try {
//封装命令行参数 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //准备环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); //创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成 Banner printedBanner = printBanner(environment); //创建ApplicationContext;决定创建web的ioc还是普通的ioc context = createApplicationContext(); analyzers = new FailureAnalyzers(context); //准备上下文环境;将environment保存到ioc中;而且applyInitializers(); //applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法 //回调所有的SpringApplicationRunListener的contextPrepared(); // prepareContext(context, environment, listeners, applicationArguments, printedBanner); //prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded(); //s刷新容器;ioc容器初始化(如果是web应用还会创建嵌入式的Tomcat);Spring注解版 //扫描,创建,加载所有组件的地方;(配置类,组件,自动配置) refreshContext(context); //从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调 //ApplicationRunner先回调,CommandLineRunner再回调 afterRefresh(context, applicationArguments); //所有的SpringApplicationRunListener回调finished方法 listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } //整个SpringBoot应用启动完成以后返回启动的ioc容器; return context; } catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); }}

转载地址:http://tzpqb.baihongyu.com/

你可能感兴趣的文章
询问HTG:安装XBMC附加组件,缩小视频以进行移动播放,自动更改默认打印机
查看>>
High Sierra推出后如何离开macOS公开Beta
查看>>
如何格式化您的WhatsApp消息
查看>>
pixel2pixel_Pixel 2的视觉核心是什么?
查看>>
更改用户账户设置自动更改_您应该更改的5个SimpliSafe设置
查看>>
excel中转换为数值_如何在Microsoft Excel中转换货币
查看>>
netflix怎么上_如何在Netflix上隐藏电视节目和电影
查看>>
opera_从Opera快速拨号页上删除混乱
查看>>
apple pencil_如何在iPad Pro的Apple Pencil上双击动作
查看>>
如何在PowerPoint中将自定义模板设置为默认模板
查看>>
linux使用命令重命名_如何在Linux上使用重命名命令
查看>>
xcloud下载_Project xCloud是Microsoft在流Xbox游戏上的赌博
查看>>
gpu驱动程序_如何从错误的GPU驱动程序更新中恢复
查看>>
esp now_Google带回Google Now(内部)排序助手
查看>>
如何防止视频在Chrome中自动播放
查看>>
如何使用Synology NAS下载文件(并避免在夜间开启计算机)
查看>>
使用批处理脚本上传ftp_通过批处理脚本将文件上传到FTP站点
查看>>
linux下如何更改主机名_如何在不重新启动的情况下更改Linux主机名
查看>>
pxe网络启动引导程序_如何使用PXE设置网络可引导实用程序光盘
查看>>
凌乱的yyy_如何清理凌乱的Internet Explorer上下文菜单
查看>>