参考此博客

    监听 boot 关闭事件

    1. import org.springframework.context.event.ContextClosedEvent;
    2. private volatile boolean isStop;
    3. @EventListener
    4. public void ContextClosedEvent(ContextClosedEvent event) {
    5. this.isStop = true;
    6. }

    获得事件之后,比如在无限循环的线程中停止线程