一:启动时完成数据加载等需求
- 实现ApplicationListener接口,官方文档截图:
ApplicationListener接口的泛型类可以使用ApplicationStartedEvent和ApplicationReadyEvent
应用监听器事件执行先后顺序如下:
- ApplicationStartingEvent
- ApplicationEnvironmentPreparedEvent
- ApplicationPreparedEvent
- ApplicationStartedEvent
- ApplicationReadyEvent
- ApplicationFailedEvent
- 实现CommandLineRunner和ApplicationRunner完成启动加载数据
二:关闭时完成某些操作
- 实现ApplicationListener<ContextClosedEvent>
实现DisposableBean接口
![](https://i-blog.csdnimg.cn/blog_migrate/5ea53c97e1e0785339f93df06012b60d.png)