springboot源码解析-管中窥豹系列之Runner

一、前言

Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去。

我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot源码管中窥豹系列。

二、Runner

假如我们想在springboot项目启动完成之后,做点什么,我们应该怎么办呢?

注意我们可以写在bean的初始化方法里面(我们后面讲),但是我们要用到其它已经加载了的bean的能力,又怎么办呢?

当然加顺序,加依赖也能解决,就是麻烦

这一节我们讨论一下springboot项目的Runner,Runner是在spring加载完毕执行的,springboot有两种Runner:

ApplicationRunner

CommandLineRunner

@FunctionalInterfacepublic interface ApplicationRunner {void run(ApplicationArguments args) throws Exception;}@FunctionalInterfacepublic interface CommandLineRunner {void run(String... args) throws Exception;}复制代码

两种除了参数不同,其它没区别

ApplicationArguments是对传参数组的封装,本质也没区别

只有执行顺序上有区别,下面源码会看到

三、用法

实现接口就可以了

import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.stereotype.Component;@Componentpublic class HelloRunner implements ApplicationRunner {        @Override    public void run(ApplicationArguments args) throws Exception {        System.out.println("hello runner");    }}复制代码

因为这时候spring已经加载完毕,你可以引入其它bean

启动项目,你会发现在日志最下方打印了上面的话

四、源码解读

我们直接找SpringApplication类的run方法,想看整体框架的去第一节。

public ConfigurableApplicationContext run(String... args) {    ...    try {                ...        callRunners(context, applicationArguments);                ...    }    catch (Throwable ex) {                ...    }    ...    return context;}复制代码

我们直接定位到callRunners方法。

private void callRunners(ApplicationContext context, ApplicationArguments args) {    List runners = new ArrayList<>();    // (1) 找到ApplicationRunner的实现类,加到list里面    runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());    // (2) 找到CommandLineRunner的实现类,加到list里面    runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());    // (3) 排序    AnnotationAwareOrderComparator.sort(runners);    // (4) 钩子回调    for (Object runner : new LinkedHashSet<>(runners)) {        if (runner instanceof ApplicationRunner) {            callRunner((ApplicationRunner) runner, args);        }        if (runner instanceof CommandLineRunner) {            callRunner((CommandLineRunner) runner, args);        }    }}复制代码

总共分四步:

(1) 找到ApplicationRunner的实现类,加到list里面

(2) 找到CommandLineRunner的实现类,加到list里面

(3) 排序

(4) 钩子回调

我们看一下canllRunner

private void callRunner(ApplicationRunner runner, ApplicationArguments args) {    try {        (runner).run(args);    }    catch (Exception ex) {        throw new IllegalStateException("Failed to execute ApplicationRunner", ex);    }}private void callRunner(CommandLineRunner runner, ApplicationArguments args) {    try {        (runner).run(args.getSourceArgs());    }    catch (Exception ex) {        throw new IllegalStateException("Failed to execute CommandLineRunner", ex);    }}复制代码

除了传参方式,都一样。

上面说的执行顺序问题,是先添加的ApplicationRunner,如果只有@Component,先执行ApplicationRunner

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章