Spring Boot程序的执行流程
Spring Boot的执行流程如下图所示:(图片来源于网络)
上图为SpringBoot启动结构图,我们发现启动流程主要分为三个部分,第一部分进行SpringApplication的初始化模块,配置一些基本的环境变量、资源、构造器、监听器,第二部分实现了应用具体的启动方案,包括启动流程的监听模块、加载配置环境模块、及核心的创建上下文环境模块,第三部分是自动化配置模块,该模块作为springboot自动配置核心,在后面的分析中会详细讨论。在下面的启动程序中我们会串联起结构中的主要功能。下面我们将从入门案例分析Spring Boot相关的原理。
@RestController
@SpringBootApplication
public class DemoApplication
{
@RequestMapping("/")
String index(){
return "Hello World!";
}
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
首先,springboot的启动类必须满足以下两个条件:
l 该类必须在项目的根目录或者父包中;
l 该类必须有@SpringBootApplication注释,这个注释说明了该类为springboot程序的启动类,是整个程序的入口;
SpringBoot程序启动时,启动类的main方法是程序的入口,执行
SpringApplication.run(DemoApplication.class, args);
该方法返回一个ConfigurableApplicationContext对象,使用注解的时候返回的具体类型是AnnotationConfigApplicationContext或AnnotationConfigEmbeddedWebApplicationContext,当支持web的时候是第二个。
当程序开始启动时,在启动类中调用SpringApplication的静态run方法,此时会执行以下操作:
1、首先新建一个SpringApplication对象;
2、然后执行对象的run()方法;
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();//新建一个StopWatch,并启动用于监测程序运行时间
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();//获取SpringApplicationRunListeners对象,并启动该监听器 Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 根据启动时传入的参数args,新建ApplicationArguments对象
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); // 新建ConfigurableEnvironment对象,具体类型为StandardServletEnvironment
this.configureIgnoreBeanInfo(environment);// 在其创建过程中将会加载springboot的配置文件application.properties,同时将其绑定的SpringApplication程序中
Banner printedBanner = this.printBanner(environment); context = this.createApplicationContext();//创建ConfigurableApplicationContext对象context
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);// 执行context的刷新操作
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
} listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
} try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
3、对配置的启动类所在包及子包中的类进行扫描,对于有spring相关注解的类,通过反射为其创建代理对象,并交由spring容器管理。
回顾整体流程,Springboot的启动,主要创建了配置环境(environment)、事件监听(listeners)、应用上下文(applicationContext),并基于以上条件,在容器中开始实例化我们需要的Bean,至此,通过SpringBoot启动的程序已经构造完成,接下来下一篇我们来探讨自动化配置是如何实现。
Spring Boot程序的执行流程的更多相关文章
- 第一章 第一个spring boot程序(转载)
第一章 第一个spring boot程序 本编博客转发自:http://www.cnblogs.com/java-zhao/p/5324185.html 环境: jdk:1.8.0_73 mave ...
- Spring Boot从入门到精通(一)搭建第一个Spring Boot程序
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...
- vc++深入跟踪MFC程序的执行流程
在MFC程序设计的学习过程中最令人感到难受,甚至于有时会动摇学习者信心的就是一种对于程序的一切细节都没有控制权的感觉.这种感觉来源于学习者不知道一个MFC程序是如何运行起来的(即一个MFC程序的执行流 ...
- 我的第一个spring boot程序(spring boot 学习笔记之二)
第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相 ...
- 深入跟踪MFC程序的执行流程
来源: http://blog.csdn.net/ljianhui/article/details/8781991 在MFC程序设计的学习过程中最令人感到难受,甚至于有时会动摇学习者信心的就是一种对于 ...
- 构建Spring Boot程序有用的文章
构建Spring Boot程序有用的文章: http://www.jb51.net/article/111546.htm
- Android开发第一讲之目录结构和程序的执行流程
1.如何在eclipse当中,修改字体 下面的这种办法,可以更改xml的字体 窗口--首选项--常规--外观--颜色和字体--基本--文本字体--编辑Window --> Preferences ...
- 3-1Java程序的执行流程
3-1Java程序的执行流程 用记事本写一个简单的程序 存到:E:\java路径下 class HelloImooc{ public static void main(String[] agrg ...
- 003 01 Android 零基础入门 01 Java基础语法 01 Java初识 03 Java程序的执行流程
003 01 Android 零基础入门 01 Java基础语法 01 Java初识 03 Java程序的执行流程 Java程序长啥样? 首先编写一个Java程序 记事本编写程序 打开记事本 1.wi ...
随机推荐
- learning makefile 模式规则
- c语言判断闰年作业
#include <stdio.h> int main() { int year,a; printf("请输人年份y:\n"); scanf("%d" ...
- 保存cookie状态封装
from urllib import request, parsefrom urllib.error import HTTPError,URLError#保存cookiefrom http impor ...
- django自定义模板标签
# 创建自定义模板标签目录 django_project_name app_name templatetags (创建Python Packge,注意一定要用templatetags这个名字) my_ ...
- java编写ID3决策树
说明:每个样本都会装入Data样本对象,决策树生成算法接收的是一个Array<Data>样本列表,所以构建测试数据时也要符合格式,最后生成的决策树是树的根节点,通过里面提供的showTre ...
- layui从子iframe打开父iframe的tab选项卡
数据表格字段: {field: 'novelId', title: '小说ID',width:100,templet: '<div><a href="javascript: ...
- JavaScript js 引入CDN 不生效 注意事项
[博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708]https ...
- Spark Streaming的容错和数据无丢失机制
spark是迭代式的内存计算框架,具有很好的高可用性.sparkStreaming作为其模块之一,常被用于进行实时的流式计算.实时的流式处理系统必须是7*24运行的,同时可以从各种各样的系统错误中恢复 ...
- 背景图片利用backgrond-posintion属性实现不同形式的分割
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- SecureCRT通过SSH2协议远程登录Ubuntu 18.04的过程总结
reference: https://blog.csdn.net/ghostar03/article/details/47441715 https://blog.csdn.net/u011186256 ...