玩转springboot之xxxRunner接口使用

Runner使用

如果需要在SpringApplication启动后执行一些逻辑,可以使用ApplicationRunner或CommandLineRunner接口,这两个接口都是只有一个run方法

public interface ApplicationRunner {

   void run(ApplicationArguments args) throws Exception;

}
public interface CommandLineRunner {

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

}

实现

@Component
public class CodeReview implements ApplicationRunner {

 @Override
 public void run(ApplicationArguments args) throws Exception {
    // 业务逻辑
  }

执行位置

该实现类是在SpringApplication.run方法最后进行执行的

afterRefresh(context, applicationArguments);


protected void afterRefresh(ConfigurableApplicationContext context,
   ApplicationArguments args)
 
{
  callRunners(context, args);
 }

 private void callRunners(ApplicationContext context, ApplicationArguments args) {
  List<Object> runners = new ArrayList<Object>();
  runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
  runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
  AnnotationAwareOrderComparator.sort(runners);
  for (Object runner : new LinkedHashSet<Object>(runners)) {
   if (runner instanceof ApplicationRunner) {
    callRunner((ApplicationRunner) runner, args);
   }
   if (runner instanceof CommandLineRunner) {
    callRunner((CommandLineRunner) runner, args);
   }
  }
 }

https://zhhll.icu/2022/框架/springboot/基础/16.Runner使用/

本文由 mdnice 多平台发布

相关推荐

  1. springbootxxxRunner接口使用

    2024-07-12 04:16:02       22 阅读
  2. springbootSpringBoot使用jsp

    2024-07-12 04:16:02       17 阅读
  3. springbootSpringApplicationRunListener

    2024-07-12 04:16:02       20 阅读
  4. springbootspringboot项目监测

    2024-07-12 04:16:02       24 阅读
  5. springbootSpringBoot单元测试

    2024-07-12 04:16:02       20 阅读
  6. springbootspringboot启动原理

    2024-07-12 04:16:02       18 阅读
  7. springbootspringboot使用外置tomcat进行运行

    2024-07-12 04:16:02       20 阅读
  8. springbootspringboot定制化tomcat

    2024-07-12 04:16:02       28 阅读
  9. 7天 Golang 标准库 sort

    2024-07-12 04:16:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 04:16:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 04:16:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 04:16:02       45 阅读
  4. Python语言-面向对象

    2024-07-12 04:16:02       55 阅读

热门阅读

  1. Spring Security的Filter

    2024-07-12 04:16:02       26 阅读
  2. WVP后端项目文件结构

    2024-07-12 04:16:02       27 阅读
  3. 贪心算法-以学籍管理系统为例

    2024-07-12 04:16:02       25 阅读
  4. RISC-V主要指令集介绍及规则

    2024-07-12 04:16:02       26 阅读
  5. 【ChatGPT】全面解析 ChatGPT:从起源到未来

    2024-07-12 04:16:02       19 阅读
  6. 代码随想录算法训练营第9天

    2024-07-12 04:16:02       23 阅读
  7. 担心插座预留的不够用,家里装修留多少开关插座

    2024-07-12 04:16:02       17 阅读
  8. Vue路由传参和接参如何实现

    2024-07-12 04:16:02       24 阅读
  9. android轮播图入门2——触摸停止与指示器

    2024-07-12 04:16:02       21 阅读