当前位置: 首页 > news >正文

Springboot项目启动成功后可通过五种方式继续执行

  1. 实现CommandLineRunner接口
    项目初始化完毕后,才会调用方法,提供服务
@Component
public class StartRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner====================");}
}
  1. 实现ApplicationRunner接口
    同 CommandLineRunner。只是传参格式不一样。CommandLineRunner:没有任何限制;ApplicationRunner:key-value
@Component
public class StartRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {System.out.println("ApplicationRunner=================");}
}
  1. 实现ApplicationListener接口
    项目初始化完毕后,才会调用方法,提供服务。注意监听的事件,通常是 ApplicationStartedEvent 或者 ApplicationReadyEvent,其他的事件可能无法注入 bean。
@Component
public class StartListener implements ApplicationListener<ApplicationStartedEvent> {@Overridepublic void onApplicationEvent(ApplicationStartedEvent event) {System.out.println("ApplicationListener================ApplicationStartedEvent");}
}

如果监听的是 ApplicationStartedEvent 事件,则 ApplicationListener 一定会在 CommandLineRunner 和 ApplicationRunner 之前执行;
如果监听的是 ApplicationReadyEvent 事件,则 ApplicationListener 一定会在 CommandLineRunner 和 ApplicationRunner 之后执行;
顺序:
默认是 ApplicationRunner 先执行,如果双方指定了@Order 则按照 @Order的大小顺序执行,小的先执行

  1. @PostConstruct注解
    在项目初始化过程中,就会调用此方法。如果业务逻辑执行很耗时,可能会导致项目启动失败。
@Component
public class StartInit {@PostConstructpublic void init() {System.out.println("@PostConstruct===============================");}}
  1. 实现InitalizingBean接口
    项目启动时,调用此方法
@Component
public class StartSet implements InitializingBean {@Overridepublic void afterPropertiesSet() {System.out.println("InitializingBean====================");}}
http://www.lryc.cn/news/253151.html

相关文章:

  • 什么是供应链金融分账系统?
  • 【测绘程序设计】——坐标换带与高程投影
  • 企业计算机服务器中了Mallox勒索病毒如何解密,Mallox勒索病毒数据恢复
  • 一套rk3588 rtsp服务器推流的 github 方案及记录 -01
  • PyQt6 QComboBox下拉组合框控件
  • 常用类与比较器
  • 【上海大学《面向对象程序设计A》课程小项目报告】抽象向量类模板及其派生类
  • Leetcode每日一题学习训练——Python3版(到达首都的最少油耗)
  • Java面试题(每天10题)-------连载(42)
  • netty websocket学习
  • 【数据结构】环形队列
  • 嵌入式C编码规范
  • Golang 并发 — 流水线
  • Elasticsearch:什么是非结构化数据?
  • 15:00的面试,15:06就出来了,问的问题过于变态了。。。
  • Web自动化测试怎么做?Web网页测试全流程解析
  • MySQL数据库SQLSTATE[22007]: Invalid datetime format 日期类型不能为空值的解决办法
  • 搬运工让你分分钟了解Web接口测试
  • 作业12.5
  • leetCode 47. 全排列 II + 回溯算法 + 图解 + 笔记
  • Maya 2024(3D建模、动画和渲染软件)
  • C++作业5
  • Go语言很难吗?为什么 Go 岗位这么少?
  • 为什么要替换 Object.defineProperty?
  • 百马百担c语言编程
  • C++检测字符串中有效的括号个数
  • 前端依赖下载速度过慢解决方法,nrm 镜像管理工具
  • 如何为 3D 模型制作纹理的最佳方法
  • 智慧校园:TSINGSEE青犀智能视频监控系统,AI助力优化校园管理
  • Three的lod技术