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 ...
随机推荐
- 理解React组件的生命周期
本文作者写作的时间较早,所以里面会出现很多的旧版ES5的时代的方法.不过,虽然如此并不影响读者理解组件的生命周期.反而是作者分为几种不同的触发机制来解释生命周期的各个方法,让读者更加容易理解涉及到的概 ...
- SQL STUFF函数 拼接字符串 多列 合并成一列 转
关于和并列的 要这种效果. create table tb(idint, value varchar(10)) insert into tbvalues(1,'aa') insert into tbv ...
- day14_雷神_前端02
# 前端day02 1. html标签 1. span标签设置宽高 设置宽高后,字体不会发生变化. 2. 盒模型 padding是border里面的距离: margin 是border边框外头的了属于 ...
- 4.BeanPostProcessor 后处理Bean
Bean种类 普通bean:之前操作的都是普通bean.<bean id="" class="A"> ,spring直接创建A实例,并返回 Fac ...
- C# 交集、差集、并集、去重
Intersect 交集,Except 差集,Union 并集 , , , , }; , , , , , }; var jiaoJi = oldArray.Intersect(newArray).To ...
- 跨站请求伪造和csrf_token使用
day70 csrf简单用法 什么是CSRF ? 跨站请求伪造, 问题: 1. 钓鱼网站的页面和正经网站的页面对浏 ...
- 机器学习技法笔记:02 Dual Support Vector Machine
Roadmap Motivation of Dual SVM Lagrange Dual SVM Solving Dual SVM Messages behind Dual SVM Summary
- Liferay7 BPM门户开发之19: 理解Service Builder体系
Service Builder是Liferay为业务开发而设计的模型驱动(model-driven)平台工具,提供一系列的实体类.数据持久化.服务相关的代码自动生成服务.支持Hibernate and ...
- Centos 7 安装 rabbitmq
1.安装erlang rabbitmq 官方下载地址 "http://www.rabbitmq.com/download.html" ,选择"RHEL, CentOS, ...
- 高手速成android开源项目【tool篇】
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...