SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展

一、Spring初始化

应用场景:在spring容器启动完成后做一些初始化的动作(如加载数据等),常见的办法有:

  1. 定义静态常量,随着类的生命周期加载而提前加载(这种方式可能对于工作经验较少的伙伴,选择是最多的);
  2. 实现CommandLineRunner接口;容器启动之后,加载实现类的逻辑资源,已达到完成资源初始化的任务;
  3. @PostConstruct;在具体Bean的实例化过程中执行,@PostConstruct注解的方法,会在构造方法之后执行;

    加载顺序为:Constructor > @Autowired > @PostConstruct > 静态方法;

    特点:

    1. 只有一个非静态方法能使用此注解
    2. 被注解的方法不得有任何参数
    3. 被注解的方法返回值必须为void
    4. 被注解方法不得抛出已检查异常
    5. 此方法只会被执行一次

  4.实现InitializingBean接口;重写afterPropertiesSet()方法;

以上方案供大家参考,提供一种解决思路。但是日常开发中有可能需要实现在项目启动后执行的功能,因此诞生了此篇文章。思路:SpringBoot提供的一种简单的实现方案,实现CommandLineRunner接口,实现功能的代码放在实现的run方法中加载,并且如果多个类需要夹加载顺序,则实现类上使用@Order注解,且value值越小则优先级越高。

二、源码跟踪

CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口:

public interface CommandLineRunner {

    /**
     * Callback used to run the bean.
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    void run(String... args) throws Exception;

}
public ConfigurableApplicationContext run(String... args) {
   StopWatch stopWatch = new StopWatch();
   //设置线程启动计时器
   stopWatch.start();
   ConfigurableApplicationContext context = null;
   Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
   //配置系统属性:默认缺失外部显示屏等允许启动
   configureHeadlessProperty();
   //获取并启动事件监听器,如果项目中没有其他监听器,则默认只有EventPublishingRunListener
   SpringApplicationRunListeners listeners = getRunListeners(args);
   //将事件广播给listeners
   listeners.starting();
   try {
       //对于实现ApplicationRunner接口,用户设置ApplicationArguments参数进行封装
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
      //配置运行环境:例如激活应用***.yml配置文件      
      ConfigurableEnvironment environment = prepareEnvironment(listeners,
            applicationArguments);
      configureIgnoreBeanInfo(environment);
      //加载配置的banner(gif,txt...),即控制台图样
      Banner printedBanner = printBanner(environment);
      //创建上下文对象,并实例化
      context = createApplicationContext();
      exceptionReporters = getSpringFactoriesInstances(
            SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
      //配置SPring容器      
      prepareContext(context, environment, listeners, applicationArguments,
            printedBanner);
      //刷新Spring上下文,创建bean过程中      
      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, listeners, exceptionReporters, ex);
      throw new IllegalStateException(ex);
   }
   listeners.running(context);
   return context;
}

 

本篇文章主要是熟悉SpringBoot的CommandLineRunner接口实现原理。因此上面SpringBoot启动过程方法不做过多介绍。我们直接进入正题CallRunners()方法内部。

callRunners方法

private void callRunners(ApplicationContext context, ApplicationArguments args) {
    //将实现ApplicationRunner和CommandLineRunner接口的类,存储到集合中
   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);
      }
   }
}

 

callRunner:

private void callRunner(CommandLineRunner runner, ApplicationArguments args) {
   try {
       //调用各个实现类中的逻辑实现
      (runner).run(args.getSourceArgs());
   }
   catch (Exception ex) {
      throw new IllegalStateException("Failed to execute CommandLineRunner", ex);
   }
}

 

关于CommandLineRunner,我们需要关注的点有两个:

  1. 所有CommandLineRunner的执行时间点是在SpringBoot应用的Application完全初始化工作之后(这里我们可以认为是SpringBoot应用启动类main方法执行完成之前的最后一步)。
  2. 当前SpringBoot应用的ApplicationContext中的所有CommandLinerRunner都会被加载执行(无论是手动注册还是被自动扫描注册到IoC容器中)。

  跟其他几个扩展点接口类型相似,我们建议CommandLineRunner的实现类使用@org.springframework.core.annotation.Order进行标注或者实现org.springframework.core.Ordered接口,便于对他们的执行顺序进行排序调整,这是非常有必要的,因为我们不希望不合适的CommandLineRunner实现类阻塞了后面其他CommandLineRunner的执行。这个接口非常有用和重要,我们需要重点关注。

 

三、示例说明

CommandLineRunner是一个接口,我们需要时,只需实现该接口就行。如果存在多个加载的数据,我们也可以使用@Order注解来排序。
案例:

加载数据库数据到缓存

package com.transsnet.palmpay.controller;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//@Component
@Order(value = 1)
public class MyStartupRunner2 implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 MyStartupRunner2 order 1 <<<<<<<<<<<<<");
    }
}

ApplicationRunner接口也可以实现上述功能

实现的方式类似的,如下:

package com.transsnet.palmpay.controller;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value = -1)
public class MyStartupRunner3 implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 MyStartupRunner3 order -1 <<<<<<<<<<<<<");
    }
}

参考:https://blog.csdn.net/xuan_lu/article/details/107849392

 

posted on 2019-07-26 17:24  duanxz  阅读(5094)  评论(0编辑  收藏  举报