【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 项目启动,开始执行...
参考:https://www.cnblogs.com/cnsyear/p/12732168.html
标签:INFO,CommandLineRunner,20,Springboot,17,启动,08,特定,ApplicationRunner 来源: https://www.cnblogs.com/HelloWxl/p/16596691.html