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 ...
随机推荐
- JRebel springboot部署idea
JRebel springboot部署idea http://127.0.0.1:8888/88414687-3b91-4286-89ba-2dc813b107ce ctrl+shift+ ...
- 使用contenteditable=true的div模拟textarea(vue2.0中使用,带placeholder且高度自动撑开)
子组件: <template> <div class="item-address"> <span v-show="!hasAddress&q ...
- python中os模块在windows下的使用
今天学习了一下Python的os模块,主要是针对文件夹和文件路径的一系列操作. 与Python内置函数相比这里这里的函数功能更多样化,功能也更强大.但是学习过程中我发现很多函数都是只适用于unix系统 ...
- spoj mpoint
题解: 判断每一次加进来的时候有几个被破坏,几个添加 然后单调栈维护 代码: #include<bits/stdc++.h> using namespace std; ; ,now,oo= ...
- 常用命令和sql
常用命令: mvn idea:idea //生成.ipr项目文件 mvn clean install -Dmaven.test.skip=true mvn install:install-file - ...
- numpy数组及处理:效率对比
def Sum(n): #定义一个函数(注意:格式对齐,否则会出错) a=list(range(n)) b=list(range(0,50000*n,5)) c=[] for i in range(l ...
- spark提交jar包时出现unsupported major.minor version 52.0错误的解决方案
一.问题: 最近在spark集群上做一个项目,打包提交jar包时,出现了unsupported major.minor version 52.0的报错,而在local模式运行却能正常运行! 二.错误原 ...
- 关于基于LinphoneSDK通话项目开发中遇到的相关问题
在之前小学期的项目开发当中,我们小组进行的是使用网上开源的LinphoneSDK来开发一款Android端的VOIP电话APP. 因为网上关于这个SDK在安卓端的开发文档相当少,所以我们只能根据少量的 ...
- react Hooks
useEffect 1.useEffect是didMount和didUpdate和willUnmount三个函数的集合 2.useEffec(fun):fun会在每次组件render之后执行,而fun ...
- java——类、对象、方法
一.类 1.Java语言把一组对象中相同属性和方法抽象到一个Java源文件就形成了类. 一个java文件可以有多个类,但是每一个类都会生成一个class字节码文件. 如果class 前加public ...