参考此博客 监听 boot 关闭事件 import org.springframework.context.event.ContextClosedEvent;private volatile boolean isStop; @EventListener public void ContextClosedEvent(ContextClosedEvent event) { this.isStop = true; } 获得事件之后,比如在无限循环的线程中停止线程