Spring Boot 中,监听应用程序启动的生命周期事件的4种方法

文章目录

  • 前言
    • [在 Spring Boot 中,监听应用程序启动的生命周期事件有多种方法。你可以使用以下几种方式来实现:](#在 Spring Boot 中,监听应用程序启动的生命周期事件有多种方法。你可以使用以下几种方式来实现:)
  • [一、使用 ApplicationListener](#一、使用 ApplicationListener)
  • [二、使用 @EventListener](#二、使用 @EventListener)
  • [三、实现 CommandLineRunner 或 ApplicationRunner](#三、实现 CommandLineRunner 或 ApplicationRunner)
  • [四、使用 SmartLifecycle](#四、使用 SmartLifecycle)
  • 总结

前言

在 Spring Boot 中,监听应用程序启动的生命周期事件有多种方法。你可以使用以下几种方式来实现:

一、使用 ApplicationListener

你可以创建一个实现 ApplicationListener 接口的类,监听 ApplicationStartingEvent、ApplicationEnvironmentPreparedEvent、ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent 等事件。

java 复制代码
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class ApplicationStartupListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("Application is ready!");
        // Your custom logic here
    }
}

二、使用 @EventListener

你可以在一个 Spring Bean 中使用 @EventListener 注解来监听这些事件。

java 复制代码
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class ApplicationStartupListener {

    @EventListener
    public void handleApplicationReady(ApplicationReadyEvent event) {
        System.out.println("Application is ready!");
        // Your custom logic here
    }
}

三、实现 CommandLineRunner 或 ApplicationRunner

这两个接口允许你在 Spring Boot 应用启动完成之后运行一些特定的代码。

  • CommandLineRunner 接收一个 String 数组作为参数。
  • ApplicationRunner 接收一个 ApplicationArguments 对象作为参数。
java 复制代码
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Application has started!");
        // Your custom logic here
    }
}

四、使用 SmartLifecycle

如果你需要更精细的控制,可以实现 SmartLifecycle 接口。这提供了一个 isAutoStartup 方法,可以控制组件是否自动启动,以及 stop 和 start 方法,可以控制组件的停止和启动。

java 复制代码
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

@Component
public class MySmartLifecycle implements SmartLifecycle {

    private boolean running = false;

    @Override
    public void start() {
        System.out.println("Starting MySmartLifecycle");
        running = true;
        // Your custom logic here
    }

    @Override
    public void stop() {
        System.out.println("Stopping MySmartLifecycle");
        running = false;
        // Your custom logic here
    }

    @Override
    public boolean isRunning() {
        return running;
    }

    @Override
    public int getPhase() {
        return 0;
    }

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable callback) {
        stop();
        callback.run();
    }
}

总结

根据你的需求,你可以选择以上任意一种方式来监听 Spring Boot 应用的启动生命周期事件。ApplicationListener 和 @EventListener 更适合处理特定的应用生命周期事件,而 CommandLineRunner 和 ApplicationRunner 更适合在应用启动后执行一些初始化逻辑。SmartLifecycle 则适合需要更精细控制生命周期的情况。

相关推荐
重庆小透明10 分钟前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
lang2015092815 分钟前
Reactor操作符的共享与复用
java
TTc_26 分钟前
@Transactional事务注解的批量回滚机制
java·事务
wei_shuo1 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
欧阳秦穆2 小时前
apoc-5.24.0-extended.jar 和 apoc-4.4.0.36-all.jar 啥区别
java·jar
岁忧2 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
Java初学者小白2 小时前
秋招Day14 - Redis - 应用
java·数据库·redis·缓存
代码老y2 小时前
Spring Boot + 本地部署大模型实现:优化与性能提升
java·spring boot·后端
GodKeyNet2 小时前
设计模式-桥接模式
java·设计模式·桥接模式
guojl3 小时前
Java多任务编排技术
java