在SpringBoot启动时执行特定代码的常见方法小结

 更新时间:2025年08月19日 09:31:17   作者:yifanghub  
本文总结了SpringBoot启动时执行代码的5种方法,涵盖@PostConstruct、CommandLineRunner、ApplicationRunner、ApplicationListener及@EventListener,各方法适用于不同场景,需要的朋友可以参考下

在SpringBoot的项目中,经常会遇到需要在项目启动后执行一些操作的情形,如加载配置,初始化数据,缓存预热等,本文整理了几种常见的在项目启动时执行特定代码的方法。

1.使用 @PostConstruct 注解

@Slf4j
@Component
public class MyInit {

    @PostConstruct
    public void init() {
        log.info("PostConstruct initialized~~~");
    }

}

优点:

  • 简单直接:只需在方法上添加 @PostConstruct 注解,方法会在Bean初始化后立即执行。
  • 灵活性高:可以在任何Bean中使用,适合对特定Bean进行初始化逻辑。

缺点:

  • 无法控制执行顺序:多个 @PostConstruct 方法的执行顺序不可控。
  • 不适合复杂启动逻辑:在Bean初始化完成后执行,但此时可能其他Bean尚未完全初始化完成,不适合依赖其他Bean的复杂场景。

2.使用 CommandLineRunner 接口

CommandLineRunner 接口提供了一个 run 方法,该方法会在Spring Boot应用启动后执行。

@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        log.info("MyStartupRunner CommandLineRunner initialized~~~");
    }
}

优点:

  • 简单易用:实现 CommandLineRunner 接口并重写 run 方法即可。
  • 适合处理命令行参数:可以直接访问命令行参数(String… args)。
  • 顺序可控:可以通过 @Order 注解或实现 Ordered 接口来控制多个 CommandLineRunner 的执行顺序。

缺点:

  • 执行时机较早:在Spring应用上下文刷新完成后执行,但此时可能某些Bean尚未完全初始化完成,不适合依赖某些复杂Bean的场景。

3.使用 ApplicationRunner 接口

ApplicationRunner 接口与 CommandLineRunner 类似,但它提供了更丰富的 ApplicationArguments 参数来处理命令行参数。

@Slf4j
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("MyApplicationRunner initialized~~~");
    }
}

优点:

  • 更强大的参数处理:提供了 ApplicationArguments 对象,可以更方便地解析命令行参数(如 --key=value 格式)。
  • 顺序可控:同样可以通过 @Order 注解或实现 Ordered 接口来控制执行顺序。

缺点:

  • 执行时机较早:与CommandLineRunner类似,可能某些Bean尚未完全初始化完成,不适合依赖某些复杂Bean的场景。

4.使用 ApplicationListener 监听 ApplicationReadyEvent

@Component
@Slf4j
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("MyApplicationListener initialized~~~");
    }
}

优点:

  • 确保应用完全启动ApplicationReadyEvent 是在应用完全启动后触发的,适合执行依赖其他Bean或外部资源的逻辑。
  • 灵活性强:可以监听其他Spring事件(如 ContextRefreshedEvent),适合更复杂的场景。

缺点:

  • 无法直接访问命令行参数:如果需要处理命令行参数,需要额外处理。

5.@EventListener监听ApplicationReadyEvent

可以使用 @EventListener 注解来监听 ApplicationReadyEvent 事件

@Component
@Slf4j
public class MyEventListener {
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        log.info("MyEventListener initialized~~~");
    }
}

优点:

  • 简洁:使用注解方式监听事件,代码更简洁。
  • 确保应用完全启动:与 ApplicationListener 相同,适合在应用完全启动后执行逻辑。
  • 灵活性强:可以监听多个事件。

缺点:

  • 无法直接访问命令行参数:同样需要额外处理命令行参数。
  • 执行顺序不可控:多个监听器的执行顺序不可控。

以上几种方式执行后日志打印如下:

[main] com.example.springdemo.init.MyInit       : PostConstruct initialized~~~
[main] c.e.springdemo.SpringDemoApplication     : Started SpringDemoApplication in 0.358 seconds (JVM running for 0.809)
[main] c.e.springdemo.init.MyApplicationRunner  : MyApplicationRunner initialized~~~
[main] c.e.springdemo.init.MyCommandLineRunner  : CommandLineRunner initialized~~~
[main] c.e.s.init.MyApplicationListener         : MyApplicationListener initialized~~~
[main] c.e.springdemo.init.MyEventListener      : MyEventListener initialized~~~

总结

  • 如果需要处理命令行参数,优先选择 CommandLineRunnerApplicationRunner
  • 如果只是简单的Bean初始化逻辑,使用 @PostConstruct
  • 如果需要在应用完全启动后执行逻辑,选择 ApplicationListener@EventListener
  • 如果需要更灵活的事件监听机制,选择 ApplicationListener@EventListener

以上就是在SpringBoot启动时执行特定代码的常见方法小结的详细内容,更多关于SpringBoot启动执行特定代码的资料请关注脚本之家其它相关文章!

相关文章

  • Springboot自带定时任务实现动态配置Cron参数方式

    Springboot自带定时任务实现动态配置Cron参数方式

    这篇文章主要介绍了Springboot自带定时任务实现动态配置Cron参数方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot异常处理之异常显示的页面问题

    SpringBoot异常处理之异常显示的页面问题

    这篇文章主要介绍了SpringBoot异常处理异常显示的页面的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • Java中解决跨域问题的方法汇总(建议收藏)

    Java中解决跨域问题的方法汇总(建议收藏)

    我们在开发过程中经常会遇到前后端分离而导致的跨域问题,导致无法获取返回结果,下面给大家介绍Java中解决跨域问题的方法汇总,感兴趣的朋友跟随小编一起看看吧
    2024-04-04
  • java使用RandomAccessFile类基于指针读写文件实例代码

    java使用RandomAccessFile类基于指针读写文件实例代码

    这篇文章主要介绍了java使用RandomAccessFile类基于指针读写文件实例代码,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • RxJava2 Scheduler使用实例深入解析

    RxJava2 Scheduler使用实例深入解析

    这篇文章主要为大家介绍了RxJava2 Scheduler使用实例深入解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • java中枚举的详细使用介绍

    java中枚举的详细使用介绍

    本篇文章介绍了,在java中枚举的详细使用。需要的朋友参考下
    2013-04-04
  • Spring实现Quartz自动配置的方法详解

    Spring实现Quartz自动配置的方法详解

    这篇文章主要介绍了Spring实现Quartz自动配置的方法详解,如果想在应用中使用Quartz任务调度功能,可以通过Spring Boot实现Quartz的自动配置,以下介绍如何开启Quartz自动配置,以及Quartz自动配置的实现过程,需要的朋友可以参考下
    2023-11-11
  • Idea如何导入java mysql驱动包

    Idea如何导入java mysql驱动包

    本文介绍了如何在IntelliJ IDEA中配置MySQL数据库连接,首先下载MySQL Connector/J驱动并解压,然后在Idea项目中创建lib文件夹并将.jar文件复制到该文件夹,接着,将.jar文件添加为项目库,通过这些步骤,可以成功配置MySQL数据库连接
    2024-12-12
  • Spring boot监控Actuator-Admin实现过程详解

    Spring boot监控Actuator-Admin实现过程详解

    这篇文章主要介绍了Spring boot监控Actuator-Admin实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 将Java应用做成exe可执行软件的流程步骤

    将Java应用做成exe可执行软件的流程步骤

    这篇文章主要介绍了如何将Java程序打包成可执行的exe文件,通过配置Maven、编写批处理文件、使用Bat_To_Exe_Converter工具将批处理文件转换为exe文件,实现双击启动的效果,感兴趣的小伙伴跟着小编一起来看看吧
    2025-04-04

最新评论