【Springboot】项目启动后执行特定方法
Springboot项目启动后执行特定方法
Springboot给我们提供了两种“开机启动”方式:ApplicationRunner和CommandLineRunner。
这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法。我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的。
CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用ApplicationArguments 用来接收参数的
使用ApplicationRunner
/**
* 项目启动执行 ApplicationRunner
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
private static Logger log = LoggerFactory.getLogger(MyCommandLineRunner.class);
@Override
public void run(ApplicationArguments var1) throws Exception{
log.info(" ApplicationRunner 项目启动,开始执行...");
}
}
使用 CommandLineRunner
/**
* 项目启动执行 CommandLineRunner
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner {
private static Logger log = LoggerFactory.getLogger(MyCommandLineRunner.class);
@Override
public void run(String... var1) throws Exception {
log.info(" CommandLineRunner 项目启动,开始执行...");
}
}
项目启动测试
2022-08-17 20:25:07.723 INFO 13704 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8089 (https) with context path '/gc'
2022-08-17 20:25:07.763 INFO 13704 --- [ restartedMain] com.chat.gc.GcChatWebrtcApplication : Started GcChatWebrtcApplication in 3.862 seconds (JVM running for 152.095)
2022-08-17 20:25:07.765 INFO 13704 --- [ restartedMain] com.chat.gc.config.MyCommandLineRunner : ApplicationRunner 项目启动,开始执行...
2022-08-17 20:25:07.765 INFO 13704 --- [ restartedMain] com.chat.gc.config.MyCommandLineRunner : CommandLineRunner 项目启动,开始执行...
2022-08-17 20:25:07.901 INFO 13704 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
指定启动方法执行的顺序
如果想要指定启动方法执行的顺序,可以通过实现org.springframework.core.Ordered接口或者使用org.springframework.core.annotation.Order注解来实现。
以ApplicationRunner 为例来分别实现
Ordered接口:
/**
* 项目启动执行 ApplicationRunner
*/
@Component
public class MyApplicationRunner implements ApplicationRunner, Ordered {
private static Logger log = LoggerFactory.getLogger(MyCommandLineRunner.class);
@Override
public void run(ApplicationArguments var1) throws Exception{
log.info(" ApplicationRunner 项目启动,开始执行...");
}
@Override
public int getOrder() {
return 1;
}
}
项目启动测试
2022-08-17 20:30:24.063 INFO 7460 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8089 (https) with context path '/gc'
2022-08-17 20:30:24.069 INFO 7460 --- [ restartedMain] com.chat.gc.GcChatWebrtcApplication : Started GcChatWebrtcApplication in 15.202 seconds (JVM running for 20.388)
2022-08-17 20:30:24.071 INFO 7460 --- [ restartedMain] com.chat.gc.config.MyCommandLineRunner : ApplicationRunner 项目启动,开始执行...
2022-08-17 20:30:24.071 INFO 7460 --- [ restartedMain] com.chat.gc.config.MyCommandLineRunner : CommandLineRunner 项目启动,开始执行...
Order注解实现方式:
/**
* 项目启动执行 CommandLineRunner
*/
@Order(value = 2)
@Component
public class MyCommandLineRunner implements CommandLineRunner {
private static Logger log = LoggerFactory.getLogger(MyCommandLineRunner.class);
@Override
public void run(String... var1) throws Exception {
log.info(" CommandLineRunner 项目启动,开始执行...");
}
}
项目启动测试
2022-08-17 20:41:36.148 INFO 696 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8089 (https) with context path '/gc'
2022-08-17 20:41:36.156 INFO 696 --- [ restartedMain] com.chat.gc.GcChatWebrtcApplication : Started GcChatWebrtcApplication in 18.13 seconds (JVM running for 22.356)
2022-08-17 20:41:36.159 INFO 696 --- [ restartedMain] com.chat.gc.config.MyCommandLineRunner : ApplicationRunner 项目启动,开始执行...
2022-08-17 20:41:36.160 INFO 696 --- [ restartedMain] com.chat.gc.config.MyCommandLineRunner : CommandLineRunner 项目启动,开始执行...
补充系统退出/关闭时监听
@Component
public class MyApplicationRunner implements ApplicationRunner, DisposableBean {
private static Logger log = LoggerFactory.getLogger(MyApplicationRunner.class);
@Override
public void run(ApplicationArguments args) throws Exception {
log.info(" ApplicationRunner 项目启动,开始执行...");
}
@Override
public void destroy() throws Exception {
log.info(" ApplicationRunner 系统停止运行,开始关闭...");
}
}
日志:
2023-02-02 15:53:35.457 INFO 304 --- [ main] usi.paofu.PaofuApplication : Started PaofuApplication in 18.515 seconds (JVM running for 25.814)
2023-02-02 15:53:35.460 INFO 304 --- [ main] usi.paofu.run.MyApplicationRunner : ApplicationRunner 项目启动,开始执行...
Disconnected from the target VM, address: '127.0.0.1:50138', transport: 'socket'
2023-02-02 15:54:38.641 INFO 304 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2023-02-02 15:54:38.645 INFO 304 --- [extShutdownHook] usi.paofu.run.MyApplicationRunner : ApplicationRunner 系统停止运行,开始关闭...
2023-02-02 15:54:38.706 INFO 304 --- [extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
参考:https://www.cnblogs.com/cnsyear/p/12732168.html
【Springboot】项目启动后执行特定方法的更多相关文章
- Springboot 项目启动后执行某些自定义代码
Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...
- Spring Boot学习--项目启动时执行特定方法
Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动 ...
- SpringBoot项目启动后再请求远程接口的实现方式
场景 有一个SpringBoot项目需要在启动后请求另一个远程服务拿取配置,而不是加载过程中去请求,可能会出现类没有实例化的场景,因此需要实现项目完全启动后再进行请求的场景. 解决 一般会有两种实现方 ...
- springboot2.X 在项目启动后执行一段自定义代码
场景: 项目需要在项目启动后从数据库初始化一些数据进入redis , 但是没有很适合 的监听器去实现 , 监听 老是在dao初始化之前触发. 解决方法:自定义类实现 ApplicationRunner ...
- 在web项目启动时执行某个方法
在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到 ...
- Springboot项目启动后自动创建多表关联的数据库与表的方案
文/朱季谦 在一些项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据 ...
- Springboot项目启动后访问不到Controller
1.搭建一个简单的Springboot项目,最开始将启动类main函数与controller放到一个类里,可以正常启动和访问,但是将两个分开再启动时访问就会报错:This application ha ...
- Java项目启动时执行指定方法的几种方式
很多时候我们都会碰到需要在程序启动时去执行的方法,比如说去读取某个配置,预加载缓存,定时任务的初始化等.这里给出几种解决方案供大家参考. 1. 使用@PostConstruct注解 这个注解呢,可以在 ...
- spring注解之@PostConstruct在项目启动时执行指定方法
一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...
- SpringBoot#应用启动后执行某些逻辑
// 方式1 @Component public class WhenStartupA implements InitializingBean { @Override public void afte ...
随机推荐
- day60:Linux压缩与打包&用户管理&用户提权sudo&grep,sed,awk,sort,uniq
目录 1.文件管理-压缩与打包 2.用户管理 用户怎么查 如何创建用户 创建的用户信息都存储在哪? 用户存储密码的文件 如何为用户设定密码? 3.用户组 4.用户提权相关 5.Extra:额外补充 文 ...
- day16:Linux常用命令
Linux中目录含义 /bin 存放普通用户的命令文件/boot 存放系统启动文件/cdrom 存放读取光盘的相关文件/dev 设备文件 /etc 配置文件/home 家目录/lib 库文件/lib6 ...
- 笔记:C++学习之旅---引用
笔记:C++学习之旅---引用 什么是引用? 引用就是别名,引用并非对象,相反的,他只是为一个已经存在的对象所起的另外一个名字. /*引用就是别名*/ #include <iostream> ...
- 一条SQL如何被MySQL架构中的各个组件操作执行的?
摘要:一条SQL如何被MySQL架构中的各个组件操作执行的,执行器做了什么?存储引擎做了什么?表关联查询是怎么在存储引擎和执行器被分步执行的?本文带你探探究竟! 本文分享自华为云社区<一条SQL ...
- 使用 shell 脚本自动申请进京证 (六环外) —— debug 过程
问题现象 用 shell 脚本写了一个自动办理六环外进京证的工具 <使用 shell 脚本自动申请进京证 (六环外)>,然而运行这个脚本总是返回以下错误信息: { "msg&qu ...
- Grafana系列-统一展示-6-Zabbix仪表板
系列文章 Grafana 系列文章 Notes: 关于 Grafana系列-统一展示-6-Zabbix 数据源, 其实已经在之前的文章: 使用 Grafana 统一监控展示 - 对接 Zabbix 里 ...
- 项目打包后配置到node服务器
1.将项目进行打包 npm run build项目根目录下会多出一个打包好的由.js .html .css文件组成的dist文件夹,如图 2.搭建node微型服务器 新建文件夹命名"no ...
- 2022-10-16:以下go语言代码输出什么?A:timed out;B:panic;C:没有任何输出。 package main import ( “context“ “fmt“
2022-10-16:以下go语言代码输出什么?A:timed out:B:panic:C:没有任何输出. package main import ( "context" &quo ...
- Casdoor 开始
Casdoor 是一个基于 OAuth 2.0 / OIDC 的中心化的单点登录(SSO)身份验证平台,简单来说,就是 Casdoor 可以帮你解决用户管理的难题,你无需开发用户登录.注册等与用户鉴权 ...
- IBM小型机 - 登录Web控制台
前言: IBM 小型机没有VGA或者HDMI接口,只能通过web或者串口的方式,配置和查看设备的硬件信息: 我们可以通过两种方式获取小型机的IP,并通过浏览器访问. 操作步骤: 1.服务器接通电源,直 ...