Spring Boot提供了两种 “开机自启动” 的方式,ApplicationRunner和CommandLineRunner

这两种方式的目的是为了满足,在容器启动时like执行某些方法。我们可以通过实现ApplicationRunner或者CommandLineRunner来实现,他们都是在SpringAppliaction执行之后开始执行的。这个特性可以让我们自定义一些在容器启动时需要初始化的逻辑

ApplicationRunner接口

官方doc:

Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple ApplicationRunner beans can be defined within the same application context and can be ordered using the Ordered

当该接口包含在SpringApplication中时执行。多个ApplicationRunner通过Order直接进行排序:

/**
* 初始化类
*/
@Order() // @Order注解可以改变执行顺序,越小越先执行
@Component
public class MyApplicationRunner1 implements ApplicationRunner { /**
* 会在服务启动完成后立即执行
*/
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("MyApplicationRunner1----" + arg0);
} }
/**
* 初始化类
*/
@Order()
@Component
public class MyApplicationRunner2 implements ApplicationRunner { /**
* 会在服务启动完成后立即执行
*/
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("MyApplicationRunner2----" + arg0);
} }

容器启动后的运行结果:

可以看到多个ApplicationRunner执行顺序是按照Order中的值执行的,并且每个的入参都是同一个ApplicationArguments实例(具体原因后面分析)

CommandLineRunner接口

二者的官方doc基本一样,区别在于接收的参数不一样

/**
* 初始化类
*/
@Order() // @Order注解可以改变执行顺序,越小越先执行
@Component
public class MyCommandLineRunner1 implements CommandLineRunner { /**
* 会在服务启动完成后立即执行
*/
@Override
public void run(String... args) throws Exception {
System.out.println("MyCommandLineRunner1----" + args);
} }
/**
* 初始化类
*/
@Order()
@Component
public class MyCommandLineRunner2 implements CommandLineRunner { /**
* 会在服务启动完成后立即执行
*/
@Override
public void run(String... args) throws Exception {
System.out.println("MyCommandLineRunner2----" + args);
} }

容器启动后的运行结果:

可以看到多个CommandLineRunner的执行效果跟ApplicationRunner一模一样

最后看下源码:

SpringApplication启动时,会执行其run方法中的afterRefresh方法:

在afterRefresh中可以看到这两个接口被执行,并且每个ApplicationRunner或CommandLineRunner实例都是用的同一个入参:

Spring Boot 初始化运行特定方法的更多相关文章

  1. 使用Spring Boot和AspectJ实现方法跟踪基础结构

    了解如何使用Spring Boot和AspectJ实现方法跟踪基础结构!最近在优锐课学习收获颇多,记录下来大家一起进步! 在我们的应用程序中,获取方法的堆栈跟踪信息可能会节省很多时间.具有输入输出参数 ...

  2. 1.spring boot初始化项目

    初始化spring boot项目的方式非常多,如使用Spring Tool Suite.使用IntelliJ IDEA.使用NetBeans.在start.spring.io网站中.curl命令.sp ...

  3. 将Spring Boot项目运行在Docker上

    将Spring Boot项目运行在Docker上 一.使用Dockerfile构建Docker镜像 1.1Dockerfile常用指令 1.1.1ADD复制文件 1.1.2ARG设置构建参数 1.1. ...

  4. VS Code打开使用IDEA搭建的Spring Boot项目运行提示"snakeyaml was not found on the classpath"错误

    今天用VS Code打开之前基于IDEA搭建并开发的Spring Boot项目,启动调试后出现如下错误: 17:43:05.214 [restartedMain] ERROR org.springfr ...

  5. 利用神器BTrace 追踪线上 Spring Boot应用运行时信息

    概述 生产环境中的服务可能会出现各种问题,但总不能让服务下线来专门排查错误,这时候最好有一些手段来获取程序运行时信息,比如 接口方法参数/返回值.外部调用情况 以及 函数执行时间等信息以便定位问题.传 ...

  6. Spring Boot定时任务运行一段时间后自动关闭的解决办法

    用Spring Boot默认支持的 Scheduler来运行定时任务,有时在服务器运行一段时间后会自动关闭.原因:Schedule默认是单线程运行定时任务的,即使是多个不同的定时任务,默认也是单线程运 ...

  7. spring容器初始化执行某个方法

    在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...

  8. Spring boot 打包瘦身方法

    背景 随着spring boot 的流行.越来越多的来发着选择使用spring boot 来发 web 应用. 不同于传统的 web 应用 需要 war 包来发布应用. spring boot 应用可 ...

  9. spring boot启动后执行方法

    @Componentpublic class InitProject implements ApplicationRunner { private static final Logger logger ...

随机推荐

  1. sshpass 使用方法

    首先下载sshpass并且安装 $ tar -zxvf sshpass-1.06.tar.gz $ cd sshpass-1.06 $ ./configure --prefix=/usr/local/ ...

  2. 【python接口自动化测试教程】00---00章节就代表开篇吧

    今天突然想写个接口测试教程,由于本人是初级的比小白稍微好那么一丢丢,所以不知道能不能坚持下来 写的不对的地方还请大咖指教 先去忙自己的工作了,忙完了回来开始写第一章吧 或者先写个大纲,要不然写的章节会 ...

  3. Codeforces828 C. String Reconstruction

    C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. json、txt、xlsx

    json:   json异于pickle,无乱码,各语言都支持,但Python各对象只直接接收int,str,(),[],{}.读入txt时只接受str,int变为str(int),()[]{}被js ...

  5. appendChild()方法遇到的问题

    在使用appendChild()方法中遇到了一个问题: 下面的代码可以正常插入多个新元素 <input type="button" value="在后面插入新元素& ...

  6. mysql大全

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  7. 去除swagger ui的红色 error 错误提示

    去除swagger ui的红色 error 错误提示 自定义js文件中加入以下的代码. 加入自定义的js方法看这里 http://www.cnblogs.com/wang2650/archive/20 ...

  8. ASP.NET Core 统一异常处理和返回

    业务场景: 业务需求要求,需要对 ASP.NET Core 异常进行统一处理和返回,比如出现 500 错误和业务服务错误进行不同的处理和返回. 具体实现: using Microsoft.AspNet ...

  9. 批量插入数据, 将DataTable里的数据批量写入数据库的方法

    大量数据导入操作, 也就是直接将DataTable里的内容写入到数据库 通用方法: 拼接Insert语句, 好土鳖 1. MS Sql Server:   使用SqlBulkCopy 2. MySql ...

  10. 每日分享!canvas的使用~

    今天大概的说下canvas的使用~ canvas是H5新增的一个元素,可以用来在canvas上绘制一些图形! 如何使用canvas呢?     首先我们用canvas绘制一条直线!   <!DO ...