SpringBoot 启动 停止 监听

SpringBoot提供了多种监听方式进行应用的启动停止监听,下面列举两种监听方式,便于在应用启动完成进行数据初始化或者在应用停止时进行垃圾回收,数据清理等等。

示例一
SpringBoot启动监听 ApplicationRunner
自定义ApplicationRunner 实现类,使用注解@Component把该实现类交于IOC管理。在SpringBoot启动成功后就会执行实现类的回调。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class ApplicationRunnerListener implements ApplicationRunner {

    private static Logger logger = LoggerFactory.getLogger(ApplicationRunnerListener.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("程序已运行");
    }
}

示例二
SpringBoot启动监听 ApplicationListener
自定义ApplicationListener 实现类,使用注解@Component把该实现类交于IOC管理。在SpringBoot启动成功后就会执行实现类的回调。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationReadyEventListener.class);

    @Override
    public void onApplicationEvent(ApplicationReadyEvent contextReadyEvent) {
        logger.info("程序已启动");
    }
}

示例三
SpringBoot停止监听 ApplicationListener
自定义ApplicationListener 实现类,使用注解@Component把该实现类交于IOC管理。在SpringBoot启动成功后就会执行实现类的回调。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

@Component
public class ApplicationClosedEventListener implements ApplicationListener<ContextClosedEvent> {
    private static Logger logger = LoggerFactory.getLogger(ApplicationClosedEventListener.class);

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        logger.info("程序已停止");
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot中,可以使用两个监听接口来实现容器启动后的回调方法:CommandLineRunner和ApplicationRunner。这两个接口都需要由程序实现,并且标识@Component注解,使其被IOC容器管理。当SpringBoot启动成功后,这些实现类的回调方法就会被执行。 除了使用监听接口,还可以通过调用AbstractMessageListenerContainer类中的start()方法来启动对消息队列的监听。该方法可以启动消息队列的消费者,使其开始监听并处理消息。这样可以在SpringBoot启动后,确保消息队列的监听器已经启动并可以消费消息。 总结起来,SpringBoot提供了多种方式来实现容器启动后的回调方法,包括监听接口和消息队列监听器的启动方法。可以根据具体的需求选择适合的方式来执行相应的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot启动监听](https://blog.csdn.net/m0_67392661/article/details/126515303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot+RabbitMQ_自定义监听容器的启动停止](https://blog.csdn.net/weixin_40877388/article/details/109514849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值