SpringBoot CommandLineRunner详解

  • A+
所属分类:SpringBoot

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

CommandLineRunner是一个接口,我们需要时,只需实现该接口就行。如果存在多个加载的数据,我们也可以使用@Order注解来排序。
案例:
分别定义了一个数据加载类MyStartupRunner1,排序为2;以及另一个数据加载类MyStartupRunner2,排序为1。

@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 <<<<<<<<<<<<<");
    }
}
--------------------- 
作者:熊秀 
来源:CSDN 
原文:https://blog.csdn.net/ruben95001/article/details/78340700?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

运行结果输出: 

SpringBoot CommandLineRunner详解

ZPY

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: