当前位置: 首页 > news >正文

CommandLineRunner 和 ApplicationRunner 用于Spring Boot 应用启动后执行特定逻辑

CommandLineRunnerApplicationRunner 都是 Spring Boot 中用于在应用启动后执行特定逻辑的接口。它们的主要区别在于传递的参数类型和执行顺序。下面我将为您详细解释它们的用途、使用案例以及执行顺序。

CommandLineRunner

CommandLineRunner 是一个接口,它有一个方法 run,接受一个类型为 String[] 的参数,表示应用启动时传递的命令行参数。通常用于执行一些与命令行参数相关的初始化操作。

使用案例:

假设您的应用需要根据命令行参数初始化一些配置,您可以使用 CommandLineRunner 来实现:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}@Component
class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {// 根据命令行参数执行初始化操作if (args.length > 0 && args[0].equals("init")) {System.out.println("执行初始化操作");}}
}

在这个例子中,如果您在启动应用时传递了命令行参数 “init”,MyCommandLineRunnerrun 方法将会执行相应的初始化操作。

ApplicationRunner

ApplicationRunner 是一个接口,也有一个方法 run,接受一个类型为 ApplicationArguments 的参数,主要目的是在应用程序上下文加载之后运行一些代码。

使用案例:

假设您的应用需要在应用程序上下文加载之后运行一些代码,您可以使用 ApplicationRunner

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}@Component
class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {// 执行初始化操作}
}

执行顺序的定义:

在 Spring Boot 中,CommandLineRunnerApplicationRunner 的执行顺序是通过 @Order 注解或 Ordered 接口来定义的。通过这些方式,您可以明确指定执行的顺序,或者让 Spring Boot 自动根据数字值进行排序。

以下是一个示例,展示了如何使用 @Order 注解来定义执行顺序:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}@Component
@Order(2)
class MyCommandLineRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner1 执行");}
}@Component
@Order(1)
class MyCommandLineRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner2 执行");}
}

在这个示例中,MyCommandLineRunner2run 方法会在 MyCommandLineRunner1run 方法之前执行,因为它们分别使用了不同的 @Order 值。

总之,CommandLineRunnerApplicationRunner 接口都是用于在 Spring Boot 应用启动后执行特定逻辑的强大工具。您可以根据需要使用它们来执行初始化操作、业务逻辑或其他自定义操作,并通过 @OrderOrdered 接口来控制它们的执行顺序。

http://www.lryc.cn/news/124161.html

相关文章:

  • 一、Dubbo 简介与架构
  • 软考:中级软件设计师:文件管理,索引文件结构,树型文件结构,位示图,数据传输方式,微内核
  • 实践-CNN卷积层
  • 【设计模式】MVC 模式
  • 看康师傅金桔柠檬X国漫IP跨界出圈,打开IP合作新思路
  • ElementUI的MessageBox的按钮置灰且不可点击
  • pc端与flutter通信失效, Method not found
  • linux 防火墙经常使用的命令
  • Docker desktop安装mysql
  • Java SpringBoot Vue ERP系统
  • 什么是CSS中的渐变(gradient)?如何使用CSS创建线性渐变和径向渐变?
  • 【深度学习】PyTorch快速入门
  • 学习Vue:组件通信
  • springboot项目打包后读取jar包里面的
  • 设计模式之七大原则
  • pytorch入门-TensorBoard和Transforms
  • 【java】Java基础——接口和实现
  • JetPack Compose 学习笔记(持续整理中...)
  • 遍历集合List的五种方法以及如何在遍历集合过程中安全移除元素
  • 【SQL应知应会】索引(二)• MySQL版
  • Android 简单的视频、图片压缩工具
  • 信息论、推理和机器学习算法之间交叉的经典例子
  • 【多线程】网络原理初识
  • Android之ADB常用命令
  • 低代码开发工具:JVS轻应用之间如何实现数据的调用?
  • 在Java中对XML的简单应用
  • Linu学习笔记——常用命令
  • PLUS操作流程、应用与实践,多源不同分辨率数据的处理、ArcGIS的应用、PLUS模型的应用、InVEST模型的应用
  • item_review-获得淘宝商品评论
  • 如何读取文件夹内的诸多文件,并选择性的保留部分文件