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

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

CommandLineRunner

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

使用案例:

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

java 复制代码
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 {

    @Override
    public void run(String... args) throws Exception {
        // 根据命令行参数执行初始化操作
        if (args.length > 0 && args[0].equals("init")) {
            System.out.println("执行初始化操作");
        }
    }
}

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

ApplicationRunner

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

使用案例:

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

java 复制代码
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 {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 执行初始化操作
    }
}

执行顺序的定义:

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

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

java 复制代码
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 {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner1 执行");
    }
}

@Component
@Order(1)
class MyCommandLineRunner2 implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner2 执行");
    }
}

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

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

相关推荐
有个傻瓜1 小时前
分页 total 对不上、列表还少几条:MyBatis-Plus 一对多 LEFT JOIN 分页错乱复盘与四种写法实测
spring boot·sql优化·mybatis-plus·分页查询·踩坑复盘
考虑考虑2 小时前
JDK27结构化并发
java·后端·java ee
沐言人生2 小时前
自媒体人的效率神器!监控、采集、下载、发布,这个免费开源工具全拿下!
前端·vue.js·后端
我叫黑大帅4 小时前
拒绝加字段堆屎山!利用位掩码与流程编排优雅落地 上图下文 复合状态
后端·算法·面试
一条Go4 小时前
Grafana 面板嵌入:为什么 kiosk 必须是「裸参数」
后端·github
Cosolar4 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
代码丰6 小时前
不依赖 LangGraph,也能实现 Human-in-the-loop:基于数据库状态机的人机协作工作流-CSDN博客
后端·架构
IT_陈寒7 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
Csvn7 小时前
Nginx 与 OpenResty:阶段钩子、shared dict 与动态 upstream/证书
后端·nginx
geovindu8 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式