前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot总结之CommandLineRunner

SpringBoot总结之CommandLineRunner

作者头像
沁溪源
发布2020-09-02 16:37:18
1.6K0
发布2020-09-02 16:37:18
举报
文章被收录于专栏:沁溪源沁溪源

一、引言

应用场景:溪源基于目前业务需求需要提前将部分数据加载到Spring容器中。大家可以想一下解决方案,下面评论去留言或者私信溪源。溪源能够想到的解决方案:

  1. 定义静态常量,随着类的生命周期加载而提前加载(这种方式可能对于工作经验较少的伙伴,选择是最多的);
  2. 实现CommandLineRunner接口;容器启动之后,加载实现类的逻辑资源,已达到完成资源初始化的任务;
  3. @PostConstruct;在具体Bean的实例化过程中执行,@PostConstruct注解的方法,会在构造方法之后执行; 加载顺序为:Constructor > @Autowired > @PostConstruct > 静态方法; 特点:
    • 只有一个非静态方法能使用此注解
    • 被注解的方法不得有任何参数
    • 被注解的方法返回值必须为void
    • 被注解方法不得抛出已检查异常
    • 此方法只会被执行一次
  4. 实现InitializingBean接口;重写afterPropertiesSet()方法;

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

二、实践

上面溪源做了简单的介绍,下面我们进入实战part。 基于CommandLineRunner接口建立两个实现类为RunnerLoadOne 、RunnerLoadTwo ;并设置加载顺序; 溪源这里使用了ClassDo对象,主要是能够体现@Order注解的加载顺序,实际应用开发中,大家根据业务需求场景适当调整(学以致用吧)。

@Component
@Order(1)
public class RunnerLoadOne implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        ClassDo classDo = SpringContextUtil.getBean(ClassDo.class);
        classDo.setClassName("Java");
        System.out.println("------------容器初始化bean之后,加载资源结束-----------");
    }
}

@Component
@Order(2)
public class RunnerLoadTwo implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        ClassDo bean = SpringContextUtil.getBean(ClassDo.class);
        System.out.println("依赖预先加载的资源数据:" + bean.getClassName());
    }
}

启动主实现类,看到console打印的结果如下:

...
2020-08-06 21:20:14.582  INFO 6612 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2020-08-06 21:20:14.592  INFO 6612 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2020-08-06 21:20:14.686  INFO 6612 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8666 (http) with context path ''
2020-08-06 21:20:14.693  INFO 6612 --- [           main] com.qxy.InformalEssayApplication         : Started InformalEssayApplication in 121.651 seconds (JVM running for 173.476)
------------容器初始化bean之后,加载资源结束-----------
依赖预先加载的资源数据:Java

三、源码跟踪

通过上面的实践操作,大家应该理解如何使用的,下面带着大家理解一下底层如何实现的; 常规操作,主启动类debugger走起来~

  • run()方法 跟进run方法后,一路F6直达以下方法
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);
      }
   }
}

上面部分代码非常简单,对于Spring源码见到如此简单逻辑代码,内心是否有一丝丝的激动~

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

到此结束,再跟进run()方法,就可以看到我们实现的资源加载逻辑啦~

总结

新手跟进源代码理解总结,若存在不当之处,希望大佬及时指正。相信经过上面简答的介绍,大家应该能够清晰的理解CommandLineRunner接口的实际应用场景了。

每天进步一点,坚持不懈,必达巅峰!!!致敬每一位始终不放弃的伙伴

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、引言
  • 二、实践
  • 三、源码跟踪
  • 总结
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档