springBoot接口:CommandLineRunner

一、作用:

在使用SpringBoot构建项目时,我们通常有一些预先数据的加载。那么SpringBoot提供了一个简单的方式来实现–CommandLineRunner。

二、用法:

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

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

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

三、展示:

springboot接口:CommandLineRunner

相关文章:

  • 2021-06-24
  • 2021-10-08
  • 2022-02-18
  • 2023-02-13
  • 1970-01-01
  • 2022-01-29
  • 2021-09-04
  • 2021-09-22
猜你喜欢
  • 2021-08-15
  • 2022-02-07
  • 2022-01-31
  • 2022-12-23
  • 2021-11-11
相关资源
相似解决方案