Spring Boot 初始化运行特定方法
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 初始化运行特定方法的更多相关文章
- 使用Spring Boot和AspectJ实现方法跟踪基础结构
了解如何使用Spring Boot和AspectJ实现方法跟踪基础结构!最近在优锐课学习收获颇多,记录下来大家一起进步! 在我们的应用程序中,获取方法的堆栈跟踪信息可能会节省很多时间.具有输入输出参数 ...
- 1.spring boot初始化项目
初始化spring boot项目的方式非常多,如使用Spring Tool Suite.使用IntelliJ IDEA.使用NetBeans.在start.spring.io网站中.curl命令.sp ...
- 将Spring Boot项目运行在Docker上
将Spring Boot项目运行在Docker上 一.使用Dockerfile构建Docker镜像 1.1Dockerfile常用指令 1.1.1ADD复制文件 1.1.2ARG设置构建参数 1.1. ...
- 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 ...
- 利用神器BTrace 追踪线上 Spring Boot应用运行时信息
概述 生产环境中的服务可能会出现各种问题,但总不能让服务下线来专门排查错误,这时候最好有一些手段来获取程序运行时信息,比如 接口方法参数/返回值.外部调用情况 以及 函数执行时间等信息以便定位问题.传 ...
- Spring Boot定时任务运行一段时间后自动关闭的解决办法
用Spring Boot默认支持的 Scheduler来运行定时任务,有时在服务器运行一段时间后会自动关闭.原因:Schedule默认是单线程运行定时任务的,即使是多个不同的定时任务,默认也是单线程运 ...
- spring容器初始化执行某个方法
在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...
- Spring boot 打包瘦身方法
背景 随着spring boot 的流行.越来越多的来发着选择使用spring boot 来发 web 应用. 不同于传统的 web 应用 需要 war 包来发布应用. spring boot 应用可 ...
- spring boot启动后执行方法
@Componentpublic class InitProject implements ApplicationRunner { private static final Logger logger ...
随机推荐
- MVC+EF(CODEFIRST)+EASYUI医药MIS系统
https://www.cnblogs.com/chenlinzhi/p/4332628.html
- Struts2学习第三天——保存登陆信息及数据校验
在JSP中服务器端保存数据的作用域通常有request.session.application,它们对应的Servlet API分别是HttpServletRquerst.HttpSession.Se ...
- Android studio提速配置
1. C:\Program Files\Android\Android Studio\bin studio64.exe.vmoptions 2.创建 gradle.properties 配置文件
- SRC列表收集
阿里asrc https://security.alibaba.com/百度bsrc http://sec.baidu.com/views/main/index.html顺丰sfsrc http:// ...
- 几种String对象方法的区别
1.在String对象方法中,发现.slice()方法和.substring()方法的作用几乎相同,都是根据起始索引返回截取得到的字符串.经过查阅资料和实测得到区别: 正常情况下索引都为正值,返回值为 ...
- git cmd 命令在已有的仓库重新添加新的文件夹
正确步骤: 1. git init //初始化仓库 git add .(文件name) //添加文件到本地仓库 git commit -m “first commit” //添加文件描述信息 git ...
- RxSwift学习笔记10:startWith/merge/zip/combineLatest/withLatestFrom/switchLatest
//startWith //该方法会在 Observable 序列开始之前插入一些事件元素.即发出事件消息之前,会先发出这些预先插入的事件消息 Observable.of(1,2,3) .startW ...
- HSmartWindowControl 之 显示图像
概述:使用Halcon在VS中的控件显示一张图片. 要点:使用了图像缩放和图像显示函数,以及鼠标滚轮响应函数. 1.创建WinForm项目 首先在VS中添加Halcon所需的控件HSmartWindo ...
- Learning WCF:A Simple Demo
This is a very simple demo which can help you create a wcf applition quickly. Create a Solution Open ...
- maven理论基础
Maven介绍 Maven是一个Java项目管理和构建工具 Maven使用pom.xml定义项目内容,并使用预设的目录结构 在Maven中声明一个依赖项可以自动下载并导入classpath Maven ...