遇到一个问题,需要从yml文件中读取数据初始化到static的类中。搜索需要实现ApplicationRunner,并在其实现类中把值读出来再set进去。于是乎就想探究一下SpringBoot启动中都干了什么。

引子

就像引用中说的,用到了ApplicationRunner类给静态class赋yml中的值。代码先量一下,是这样:

@Data
@Component
@EnableConfigurationProperties(MyApplicationRunner.class)
@ConfigurationProperties(prefix = "flow")
public class MyApplicationRunner implements ApplicationRunner { private String name;
private int age;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner...start...");
MyProperties.setAge(age);
MyProperties.setName(name);
System.out.println("ApplicationRunner...end...");
}
}
public class  MyProperties {
private static String name;
private static int age;
public static String getName() {
return name;
}
public static void setName(String name) {
MyProperties.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
MyProperties.age = age;
}
}

从SpringApplication开始

@SpringBootApplication
public class FlowApplication {
public static void main(String[] args) {
SpringApplication.run(FlowApplication.class, args);
}
}

这是一个SpringBoot启动入口,整个项目环境搭建和启动都是从这里开始的。我们就从SpringApplication.run()点进去看一下,Spring Boot启动的时候都做了什么。点进去run看一下。

public static ConfigurableApplicationContext run(Class<?> primarySource,
String... args) {
return run(new Class<?>[] { primarySource }, args);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}

首先经过了两个方法,马上就要进入关键了。SpringApplication(primarySources).run(args),这句话做了两件事,首先初始化SpringApplication,然后进行开启run。首先看一下初始化做了什么。

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}

首先读取资源文件Resource,然后读取FlowApplication这个类信息(就是primarySources),然后从classPath中确定是什么类型的项目,看一眼WebApplicationType这里面有三种类型:

public enum WebApplicationType {
NONE, //不是web项目
SERVLET,//是web项目
REACTIVE;//2.0之后新加的,响应式项目
...
}

回到SpringApplication接着看,确定好项目类型之后,初始化一些信息setInitializers(),getSpringFactoriesInstances()看一下都进行了什么初始化:

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = getClassLoader();
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<>(
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}

首先得到ClassLoader,这个里面记录了所有项目package的信息、所有calss的信息啊什么什么的,然后初始化各种instances,在排个序,ruturn之。

再回到SpringApplication,接着是设置监听器setListeners()。

然后设置main方法,mainApplicationClass(),点进deduceMainApplicationClass()看一看:

private Class<?> deduceMainApplicationClass() {
try {
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if ("main".equals(stackTraceElement.getMethodName())) {
return Class.forName(stackTraceElement.getClassName());
}
}
}
catch (ClassNotFoundException ex) {
// Swallow and continue
}
return null;
}

从方法栈stackTrace中,不断读取方法,通过名称,当读到“main”方法的时候,获得这个类实例,return出去。

到这里,所有初始化工作结束了,也找到了Main方法,ruturn给run()方法,进行后续项目的项目启动。

准备好,开始run吧

先上代码:

public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
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;
}

首先开启一个计时器,记录下这次启动时间,咱们项目开启 XXXms statred 就是这么计算来的。

然后是一堆声明,知道listeners.starting(),这个starting(),我看了一下源码注释

Called immediately when the run method has first started. Can be used for very

early initialization.

早早初始化,是为了后面使用,看到后面还有一个方法listeners.started()

Called immediately before the run method finishes, when the application context has been refreshed and all {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner ApplicationRunners} have been called.

这会儿应该才是真正的开启完毕,值得一提的是,这里终于看到了引子中的ApplicationRunner这个类了,莫名的有点小激动呢。

我们继续进入try,接下来是读取一些参数applicationArguments,然后进行listener和environment的一些绑定。然后打印出Banner图,printBanner(),这个方法里面可以看到把environment,也存入Banner里面了,应该是为了方便打印,如果有日志模式,也打印到日志里面,所以,项目启动的打印日志里面记录了很多东西。

private Banner printBanner(ConfigurableEnvironment environment) {
if (this.bannerMode == Banner.Mode.OFF) {
return null;
}
ResourceLoader resourceLoader = (this.resourceLoader != null)
? this.resourceLoader : new DefaultResourceLoader(getClassLoader());
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
resourceLoader, this.banner);
if (this.bannerMode == Mode.LOG) {
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
}
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}

接着 生成上下文环境 context = createApplicationContext();还记着webApplicationType三种类型吗,这边是根据webApplicationType类型生成不同的上下文环境类的。

接着开启 exceptionReporters,用来支持启动时的报错。

接着就要准备往上下文中set各种东西了,看prepareContext()方法:

private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}

首先把环境environment放进去,然后把resource信息也放进去,再让所有的listeners知道上下文环境。 这个时候,上下文已经读取yml文件了 所以这会儿引子中yml创建的参数,上下文读到了配置信息,又有点小激动了!接着看,向beanFactory注册单例bean:一个参数bean,一个Bannerbean。

prepareContext() 这个方法大概先这样,然后回到run方法中,看看项目启动还干了什么。

refreshContext(context) 接着要刷新上下问环境了,这个比较重要,也比较复杂,今天只看个大概,有机会另外写一篇博客,说说里面的东西,这里面主要是有个refresh()方法。看注释可知,这里面进行了Bean工厂的创建,激活各种BeanFactory处理器,注册BeanPostProcessor,初始化上下文环境,国际化处理,初始化上下文事件广播器,将所有bean的监听器注册到广播器(这样就可以做到Spring解耦后Bean的通讯了吧)

总之,Bean的初始化我们已经做好了,他们直接也可以很好的通讯。

接着回到run方法,

afterRefresh(context, applicationArguments); 这方法里面没有任何东西,网上查了一下,说这里是个拓展点,有机会研究下。

接着stopWatch.stop();启动就算完成了,因为这边启动时间结束了。

我正要失落的发现没找到我们引子中说到的ApplicationRunner这个类,就在下面看到了最后一个方法,必须贴出来源码:

callRunners(context, applicationArguments),当然这个方法前面还有listeners.started().

private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList<>();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
for (Object runner : new LinkedHashSet<>(runners)) {
if (runner instanceof ApplicationRunner) {
callRunner((ApplicationRunner) runner, args);
}
if (runner instanceof CommandLineRunner) {
callRunner((CommandLineRunner) runner, args);
}
}
}

看到没,这会就执行了ApplicationRunner 方法(至于CommandLineRunner,和ApplicationRunner类似,只是参数类型不同,这边不做过多区分先)。所以可以说ApplicationRunner不是启动的一部分,不记录进入SpringBoot启动时间内,这也好理解啊,你自己初始化数据的时间凭什么算到我SpringBoot身上,你要初始化的时候做了个费时操作,回头又说我SpringBoot辣鸡,那我不是亏得慌...

最后run下这个,listeners.running(context);这会儿用户自定义的事情也会被调用了。

ok,结束了。

小结

今天只是大概看了下SpringBoot启动过程。有很多细节,比如refresh()都值得再仔细研究一下。SpringBoot之所以好用,就是帮助我们做了很多配置,省去很多细节(不得不说各种stater真实让我们傻瓜式使用了很多东西),但是同样定位bug或者通过项目声明周期搞点事情的时候会无从下手。所以,看看SpringBoot源码还是听有必要的。

源码分析SpringBoot启动的更多相关文章

  1. Appium Server 源码分析之启动运行Express http服务器

    通过上一个系列Appium Android Bootstrap源码分析我们了解到了appium在安卓目标机器上是如何通过bootstrap这个服务来接收appium从pc端发送过来的命令,并最终使用u ...

  2. Appium Android Bootstrap源码分析之启动运行

    通过前面的两篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>和<Appium Android Bootstrap源码分析之命令解析 ...

  3. Linux内核源码分析--内核启动之(3)Image内核启动(C语言部分)(Linux-3.0 ARMv7)

    http://blog.chinaunix.net/uid-20543672-id-3157283.html Linux内核源码分析--内核启动之(3)Image内核启动(C语言部分)(Linux-3 ...

  4. Linux内核源码分析--内核启动之(6)Image内核启动(do_basic_setup函数)(Linux-3.0 ARMv7)【转】

    原文地址:Linux内核源码分析--内核启动之(6)Image内核启动(do_basic_setup函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://bl ...

  5. Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7)【转】

    原文地址:Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.c ...

  6. u-boot 源码分析(1) 启动过程分析

    u-boot 源码分析(1) 启动过程分析 文章目录 u-boot 源码分析(1) 启动过程分析 前言 配置 源码结构 api arch board common cmd drivers fs Kbu ...

  7. v87.01 鸿蒙内核源码分析 (内核启动篇) | 从汇编到 main () | 百篇博客分析 OpenHarmony 源码

    本篇关键词:内核重定位.MMU.SVC栈.热启动.内核映射表 内核汇编相关篇为: v74.01 鸿蒙内核源码分析(编码方式) | 机器指令是如何编码的 v75.03 鸿蒙内核源码分析(汇编基础) | ...

  8. 安卓MonkeyRunner源码分析之启动

    在工作中因为要追求完成目标的效率,所以更多是强调实战,注重招式,关注怎么去用各种框架来实现目的.但是如果一味只是注重招式,缺少对原理这个内功的了解,相信自己很难对各种框架有更深入的理解. 从几个月前开 ...

  9. Django源码分析之启动wsgi发生的事

    前言 ​ 好多人对技术的理解都停留在懂得使用即可,因而只会用而不会灵活用,俗话说好奇害死猫,不然我也不会在凌晨1.48的时候决定写这篇博客,好吧不啰嗦了 ​ 继续上一篇文章,后我有个问题(上文:&qu ...

随机推荐

  1. 基于百度语音识别API的Python语音识别小程序

    一.功能概述 实现语音为文字,可以扩展到多种场景进行工作,这里只实现其基本的语言接收及转换功能. 在语言录入时,根据语言内容的多少与停顿时间,自动截取音频进行转换. 工作示例: 二.软件环境 操作系统 ...

  2. OpenStack(四)——使用Kolla部署OpenStack多节点云

    (1).实验环境 主机名 IP地址 角色 内存 网卡 CPU 磁盘 OpenStack-con 192.168.128.110 controller(控制) 8G 桥接网卡ens32和ens33 4核 ...

  3. ES6 之 函数的扩展 尾调用以及尾递归

    函数参数的默认值 function log(x, y) { y = y || 'world' console.log(x + ' ' + y); } log('hello') // hello wor ...

  4. C++queue的使用

    C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...

  5. 存储基本概念(lun,volume,HBA,DAS,NAS,SAN,iSCSI,IPSAN)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/liukuan73/article/det ...

  6. Servlet基本概念及其部署

    什么servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动 ...

  7. 27. docker compose 单机 均衡负载

    1.编写Dockerfile #Dockerfile FROM python:2.7 LABEL maintaner="eaon eaon123@docker.com" COPY ...

  8. 03 Mybatis:05.使用Mybatis完成CRUD

    mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...

  9. Python dict 字典 keys和values对换

    原字典: d1 = { 'en':'英语', 'cn':'中文', 'fr':'法语', 'jp':'日语' } 经过相互对换: d1_inverse = {values:keys for keys, ...

  10. Python—插入排序算法

    # 插入排序,时间复杂度O(n²) def insert_sort(arr): """ 插入排序:以朴克牌为例,从小到大排序.摸到的牌current与手里的每张牌进行对比 ...