SpringBoot启动后执行方法

jupiter
2024-06-22 / 0 评论 / 107 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年06月22日,已超过182天没有更新,若内容或图片失效,请留言反馈。

0.主启动类

public class HikariDataSourceApplication {
    public static void main(String[] args) {
        SpringApplication.run(HikariDataSourceApplication.class, args);
        System.out.println("项目启动成功=====================================");
    }
}

1.@PostConstruct 注解

在项目初始化过程中,就会调用此方法。如果业务逻辑执行很耗时,可能会导致项目启动失败。

import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Component;

@Component
public class StartInit1 {
    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct===============================");
    }
}
2024-06-22T22:51:22.978+08:00  INFO 18120 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-06-22T22:51:22.990+08:00  INFO 18120 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-22T22:51:22.990+08:00  INFO 18120 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-06-22T22:51:23.023+08:00  INFO 18120 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-22T22:51:23.024+08:00  INFO 18120 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 668 ms
@PostConstruct===============================
2024-06-22T22:51:23.303+08:00  WARN 18120 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - idleTimeout is close to or more than maxLifetime, disabling it.
2024-06-22T22:51:23.304+08:00  INFO 18120 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-06-22T22:51:23.391+08:00  INFO 18120 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@46468f0
2024-06-22T22:51:23.393+08:00  INFO 18120 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-06-22T22:51:23.503+08:00  INFO 18120 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-06-22T22:51:23.509+08:00  INFO 18120 --- [           main] c.e.h.HikariDataSourceApplication        : Started HikariDataSourceApplication in 1.415 seconds (process running for 1.729)
项目启动成功=====================================

2.实现 CommandLineRunner 接口

项目初始化完毕后,才会调用方法,提供服务

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartInit2 implements CommandLineRunner {
    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunner====================");
    }
}
2024-06-22T22:54:34.798+08:00  INFO 14312 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-06-22T22:54:34.806+08:00  INFO 14312 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-22T22:54:34.806+08:00  INFO 14312 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-06-22T22:54:34.845+08:00  INFO 14312 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-22T22:54:34.845+08:00  INFO 14312 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 660 ms
2024-06-22T22:54:35.128+08:00  WARN 14312 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - idleTimeout is close to or more than maxLifetime, disabling it.
2024-06-22T22:54:35.128+08:00  INFO 14312 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-06-22T22:54:35.214+08:00  INFO 14312 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@3a0b6a
2024-06-22T22:54:35.215+08:00  INFO 14312 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-06-22T22:54:35.324+08:00  INFO 14312 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-06-22T22:54:35.328+08:00  INFO 14312 --- [           main] c.e.h.HikariDataSourceApplication        : Started HikariDataSourceApplication in 1.408 seconds (process running for 1.722)
CommandLineRunner====================
项目启动成功=====================================    

3.实现 ApplicationRunner 接口

CommandLineRunner。只是传参格式不一样。CommandLineRunner:没有任何限制;ApplicationRunner:key-value

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class StartInit3 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) {
        System.out.println("ApplicationRunner=================");
    }
}
2024-06-22T22:57:50.064+08:00  INFO 19228 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-06-22T22:57:50.071+08:00  INFO 19228 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-22T22:57:50.071+08:00  INFO 19228 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-06-22T22:57:50.105+08:00  INFO 19228 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-22T22:57:50.105+08:00  INFO 19228 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 670 ms
2024-06-22T22:57:50.386+08:00  WARN 19228 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - idleTimeout is close to or more than maxLifetime, disabling it.
2024-06-22T22:57:50.387+08:00  INFO 19228 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-06-22T22:57:50.481+08:00  INFO 19228 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@539c4830
2024-06-22T22:57:50.483+08:00  INFO 19228 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-06-22T22:57:50.601+08:00  INFO 19228 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-06-22T22:57:50.606+08:00  INFO 19228 --- [           main] c.e.h.HikariDataSourceApplication        : Started HikariDataSourceApplication in 1.439 seconds (process running for 1.755)
ApplicationRunner=================
项目启动成功=====================================

4.实现 ApplicationListener 接口

项目初始化完毕后,才会调用方法,提供服务。注意监听的事件,通常是 ApplicationStartedEvent 或者 ApplicationReadyEvent,其他的事件可能无法注入 bean。

  • 如果监听的是 ApplicationStartedEvent 事件,则 ApplicationListener 一定会在 CommandLineRunner 和 ApplicationRunner 之前执行;
  • 如果监听的是 ApplicationReadyEvent 事件,则 ApplicationListener 一定会在 CommandLineRunner 和
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class StartInit4 implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.out.println("ApplicationListener================ApplicationStartedEvent");
    }
}
2024-06-22T23:01:14.615+08:00  INFO 21164 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-06-22T23:01:14.627+08:00  INFO 21164 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-22T23:01:14.628+08:00  INFO 21164 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-06-22T23:01:14.661+08:00  INFO 21164 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-22T23:01:14.661+08:00  INFO 21164 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 683 ms
2024-06-22T23:01:14.942+08:00  WARN 21164 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - idleTimeout is close to or more than maxLifetime, disabling it.
2024-06-22T23:01:14.942+08:00  INFO 21164 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-06-22T23:01:15.027+08:00  INFO 21164 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@6486fe7b
2024-06-22T23:01:15.028+08:00  INFO 21164 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-06-22T23:01:15.133+08:00  INFO 21164 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-06-22T23:01:15.138+08:00  INFO 21164 --- [           main] c.e.h.HikariDataSourceApplication        : Started HikariDataSourceApplication in 1.429 seconds (process running for 1.752)
ApplicationListener================ApplicationStartedEvent
项目启动成功=====================================

5.执行顺序

2024-06-22T23:03:22.206+08:00  INFO 19432 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-22T23:03:22.207+08:00  INFO 19432 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-06-22T23:03:22.244+08:00  INFO 19432 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-22T23:03:22.244+08:00  INFO 19432 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 613 ms
@PostConstruct===============================
2024-06-22T23:03:22.519+08:00  WARN 19432 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - idleTimeout is close to or more than maxLifetime, disabling it.
2024-06-22T23:03:22.519+08:00  INFO 19432 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-06-22T23:03:22.604+08:00  INFO 19432 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@3e14d390
2024-06-22T23:03:22.605+08:00  INFO 19432 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-06-22T23:03:22.708+08:00  INFO 19432 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-06-22T23:03:22.716+08:00  INFO 19432 --- [           main] c.e.h.HikariDataSourceApplication        : Started HikariDataSourceApplication in 1.317 seconds (process running for 1.573)
ApplicationListener================ApplicationReadyEvent
ApplicationRunner=================
CommandLineRunner====================
ApplicationListener================ApplicationReadyEvent
项目启动成功=====================================

参考资料

  1. 【SpringBoot】 启动后执行方法的五种方式_springboot启动后执行某个方法-CSDN博客
  2. Springboot启动后执行方法的四种方式_springboot 启动执行方法-CSDN博客
0

评论 (0)

打卡
取消