CommandLineRunner解释学习

目录

一、CommandLineRunner介绍

1.1 接口定义

1.2 工作原理

二、CommandLineRunner作用

三、CommandLineRunner使用方式

3.1 实现CommandLineRunner

3.2 配置Spring Boot项目

四、完整代码地址

小剧场:坚持不懈!


一、CommandLineRunner介绍

CommandLineRunner是Spring Boot提供的一个便捷接口,它允许开发者在Spring应用程序启动后执行一些特定的代码。这个接口特别适用于那些需要在服务器启动时立即执行的任务,比如数据初始化、发送通知或者执行一些定时任务。

1.1 接口定义

CommandLineRunner接口只包含一个方法run,这个方法将在Spring应用程序启动后被调用。开发者可以实现这个接口并定义run方法的具体逻辑。

package org.springframework.boot.CommandLineRunner;
public interface CommandLineRunner {
    void run(String... args) throws Exception;
}

1.2 工作原理

当Spring Boot应用程序启动时,Spring容器会查找所有实现了CommandLineRunner接口的Bean,并在容器启动完成后调用这些Bean的run方法。如果有多个CommandLineRunner实现,它们的执行顺序将根据实现类的名称进行排序。

二、CommandLineRunner作用

CommandLineRunner的主要作用是在应用程序启动时执行一些特定的任务,这些任务通常包括:

  • 数据初始化:在应用程序启动时加载一些初始数据到数据库中。
  • 发送通知:向用户或系统发送启动通知,例如发送邮件或消息队列中的消息。
  • 执行检查:进行应用程序启动前的检查,如数据库连接检查、文件系统检查等。
  • 启动日志:记录应用程序启动日志,提供启动时的系统状态信息。
  • 定时任务:启动一些定时任务,如清理旧数据、备份操作等。

三、CommandLineRunner使用方式

要使用CommandLineRunner,您需要创建一个实现了CommandLineRunner接口的类,并实现run方法。然后将这个类声明为Spring容器中的Bean,Spring Boot将在启动时自动调用它。

3.1 实现CommandLineRunner

完整的代码示例位置:https://gitee.com/zhulinxishui/SpringBoot-CommandLineRunner.git

下面是一部分的CommandLineRunner实现示例:

package com.wxr.config;
import com.wxr.service.AsyncTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @ClassName initRunnable
 * @Description TODO
 * @Author wangxinrui
 * @Date 2024/3/25 19:22
 * @Version
 **/
@Component
@Order(2)
public class InitRunnable implements CommandLineRunner {
    @Autowired
    private AsyncTaskService asyncTaskService;

    /**
     *@描述 重写一下run方法,编写自己需要的逻辑。
     *@用途 此方法一般用于创建默认数据,加载配置文件,初始化数据库操作等。 可以实现CommandLineRunner接口,在run方法中加载一些初始化数据到数据库等。适合做一些数据预加载工作。
     *@执行时间 在系统启动完成后加载执行。
     *@创建人 wangxinrui
     *@创建时间 2024/3/25
     *@修改人和其它信息
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println("setup3");
        /**可以在这个线程中异步执行其他现场,很好的增强了相关功能*/
        new Thread(()->{
            asyncTaskService.doTaskOne();
            asyncTaskService.doTaskTwo();
            asyncTaskService.doTaskThree();
        }).start();
        /** 也可以使用线程池来做相关的操作*/
        Executor executor = Executors.newCachedThreadPool();
        executor.execute(new ExecutorRunable("10000", asyncTaskService));

        /** 使用带有返回值的线程处理。 */
        Callable<String> task = () ->{
            // 这里是你的异步任务
            String result = asyncTaskService.doTaskFive();
            return result;
        };
        ExecutorService executorSingle = Executors.newSingleThreadExecutor();
        Future<String> future =  executorSingle.submit(task);
        String result = future.get();
        System.out.println(result);
    }
}

3.2 配置Spring Boot项目

为了确保Spring Boot能够扫描到我们的MyRunner类并将其作为Bean注册到容器中,我们需要在项目中添加Spring Boot的依赖。

在pom.xml中添加以下依赖:

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- 其他依赖... -->
</dependencies>

通过这种方式,您可以在Spring Boot应用程序中轻松地使用CommandLineRunner来执行启动时的任务。

四、完整代码地址

代码地址:https://gitee.com/zhulinxishui/SpringBoot-CommandLineRunner.git

小剧场:坚持不懈!

  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值