遇到一个问题,需要从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. 为什么ApplePay在中国一直火不起来?

    今年7月,易观发布<中国第三方移动支付市场2018年第1季度监测报告>.报告显示,2018年第一季度支付宝以53.76%的市场份额占据移动支付头把交椅,腾讯金融(微信支付)则以38.95% ...

  2. 将list等分成n份

    public static <T> Map<Integer, List<T>> spiltList(List<T> list, int num) { M ...

  3. poj 3013 最短路SPFA算法

    POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23630 ...

  4. 201771010123汪慧和《面向对象程序设计Java》第二周学习总结

    一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...

  5. MySQL--InnoDB 关键特性

    插入缓冲 Insert Buffer 对于非聚集索引的插入或更新操作,不是每一次直接插入到索引页中,而是先判断插入的非聚集索引页是否在缓冲池中,若在,则直接插入:若不在,则先放入到一个 Insert ...

  6. vue安装openlayers,jquery,bootstrap,阿里iconfont,

    安装 安装openlayers安装指定包安装openlayersVUE中的地图import ol from "openlayers";import "openlayers ...

  7. SQL 一个【OR】属性给你带来了怎样的麻烦

    关于 数据库  or   的用法,就不说了     讲讲OR对数据库性能有多大的杀伤力,以后线上的项目如果是数据量很大的表,一定不要用OR来查询 今天在工作的时候,同事遇到了SQL优化的问题,让我帮忙 ...

  8. 吴裕雄--天生自然ShellX学习笔记:Shell 传递参数

    在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以下实例我们向脚本传递三个参数, ...

  9. zip和rar文件的contentType

    zip.rar.msi等文件使用Chrome提交的ContentType都为null,IE可以正确获取

  10. [mark]C# 异常处理

    https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/exceptions/index