springboot接口:CommandLineRunner

Posted xujie09

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot接口:CommandLineRunner相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

springboot集成webservice接口

springBoot接口排队(串行执行)

SpringBoot:设置springboot同一接口程序启动入口

springboot接口:CommandLineRunner

springboot 项目接口调用失败

Springboot系列 集成接口文档swagger,使用,测试