• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

JAVA相关 丰极 2941次浏览 0个评论

一、前言

  • Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去。
  • 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot源码管中窥豹系列。

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

二、Runner

  • 假如我们想在springboot项目启动完成之后,做点什么,我们应该怎么办呢?
  • 注意我们可以写在bean的初始化方法里面(我们后面讲),但是我们要用到其它已经加载了的bean的能力,又怎么办呢?
  • 当然加顺序,加依赖也能解决,就是麻烦

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

  • ApplicationRunner
  • CommandLineRunner
@FunctionalInterface
public interface ApplicationRunner {

	void run(ApplicationArguments args) throws Exception;

}

@FunctionalInterface
public interface CommandLineRunner {

	void run(String... args) throws Exception;

}

  • 两种除了参数不同,其它没区别
  • ApplicationArguments是对传参数组的封装,本质也没区别
  • 只有执行顺序上有区别,下面源码会看到

三、用法

实现接口就可以了

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public 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<Object> 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

目录
springboot源码解析-管中窥豹系列之总体结构(一)
springboot源码解析-管中窥豹系列之项目类型(二)
springboot源码解析-管中窥豹系列之Runner(三)
springboot源码解析-管中窥豹系列之Initializer(四)
springboot源码解析-管中窥豹系列之排序(五)
springboot源码解析-管中窥豹系列之aware(六)
springboot源码解析-管中窥豹系列之web服务器(七)
springboot源码解析-管中窥豹系列之BeanDefinition(八)
springboot源码解析-管中窥豹系列之自动装配(九)
springboot源码解析-管中窥豹系列之EnableXXX(十)
springboot源码解析-管中窥豹系列之BeanFactoryPostProcessor(十一)
springboot源码解析-管中窥豹系列之BeanPostProcessor(十二)

欢迎关注公众号:丰极,更多技术学习分享。


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明springboot源码解析-管中窥豹系列之Runner(三)
喜欢 (0)

您必须 登录 才能发表评论!

加载中……