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

springboot 启动时就执行特定接口

在 Spring Boot 中,有多种方式可以在应用启动时自动执行特定代码(如调用接口)。以下是几种常用方法及适用场景:


🚀 方法一:CommandLineRunner(推荐)

适用于启动后立即执行一次性任务。

import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class StartupRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Spring Boot 启动完成,执行特定接口..."); // 调用你的接口逻辑 callYourApi(); } private void callYourApi() { // 你的接口调用代码 System.out.println("接口调用成功!"); } }

特点

  • 简单直接
  • 可注入其他 Bean
  • 支持多个CommandLineRunner,通过@Order控制执行顺序

🎯 方法二:ApplicationRunner(推荐)

CommandLineRunner类似,但参数封装为ApplicationArguments,更易处理。

import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class AppStartupRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("应用启动,执行接口调用..."); callYourApi(); } private void callYourApi() { // 接口调用逻辑 } }

📌 方法三:@PostConstruct(Bean 初始化后执行)

适用于某个特定 Bean 初始化完成后执行。

import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class InitService { @PostConstruct public void init() { System.out.println("Bean 初始化完成,执行接口调用..."); callYourApi(); } private void callYourApi() { // 接口调用逻辑 } }

特点

  • 在依赖注入完成后、@Autowired之后执行
  • 适合与特定 Bean 绑定的初始化逻辑

🔔 方法四:监听ApplicationReadyEvent(应用完全就绪后)

适用于需要在 Spring 容器完全初始化、所有 Bean 就绪后再执行的场景。

import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class AppReadyListener { @EventListener(ApplicationReadyEvent.class) public void onApplicationReady() { System.out.println("应用完全就绪,执行接口调用..."); callYourApi(); } private void callYourApi() { // 接口调用逻辑 } }

🛠 方法五:ApplicationListener监听事件

传统方式,监听ContextRefreshedEvent

import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @Component public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().getParent() == null) { // 防止父容器触发多次 System.out.println("上下文刷新完成,执行接口调用..."); callYourApi(); } } private void callYourApi() { // 接口调用逻辑 } }

📊 各方法执行顺序对比

方法执行时机适用场景
@PostConstructBean 初始化后与特定 Bean 绑定的初始化
CommandLineRunner / ApplicationRunnerSpring 容器启动后,ApplicationReadyEvent 之前一次性启动任务
ApplicationReadyEvent所有 Bean 就绪,应用完全可用需要完整上下文的任务
ContextRefreshedEvent上下文刷新完成传统方式,兼容性好

🎨 示例:调用外部 HTTP 接口

import org.springframework.boot.CommandLineRunner; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class ApiCallRunner implements CommandLineRunner { private final RestTemplate restTemplate; public ApiCallRunner(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @Override public void run(String... args) throws Exception { String url = "http://api.example.com/endpoint"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); if (response.getStatusCode().is2xxSuccessful()) { System.out.println("接口调用成功: " + response.getBody()); } else { System.err.println("接口调用失败: " + response.getStatusCode()); } } }

⚙️ 配置 RestTemplate Bean(如需)

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class AppConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }

✅ 总结推荐

需求推荐方法
简单启动任务CommandLineRunner 或 ApplicationRunner
需要完整上下文ApplicationReadyEvent
与特定 Bean 绑定@PostConstruct
需要控制执行顺序多个 CommandLineRunner + @Order
http://www.jsqmd.com/news/358378/

相关文章:

  • 2026年经济型加工中心、精密加工中心哪家供应商价格实惠且靠谱 - 工业品牌热点
  • Flutter for OpenHarmony:魔方计时器开发实战 - 基于Flutter的专业番茄工作法应用实现与交互设计
  • 诺丁山婚礼一站式筹备费用多少,应急处理能力靠谱吗 - myqiye
  • Flutter for OpenHarmony:极简笔记应用开发全指南 - 从实现到设计理念
  • 探讨2026年鄞州区一站式婚礼机构,求推荐口碑好的品牌 - 工业推荐榜
  • 聊聊事业单位考试资深培训企业 中志教育性价比如何 - mypinpai
  • 2026加工中心实力供应商,靠谱的品牌怎么选 - 工业品牌热点
  • 这些乘务学校值得选服务出众,上海区域口碑良好 - 工业设备
  • 【开题答辩全过程】以 基于SSM的好物推荐系统的设计与实现为例,包含答辩的问题和答案
  • 真心不骗你 9个AI论文平台测评:本科生毕业论文+科研写作必备工具推荐
  • 2026年湖南源头烟花秀厂家排名,哪家口碑好? - myqiye
  • GESP2025年6月认证C++二级( 第二部分判断题(1-10))
  • 2026广州好吃白切鸡餐厅:体育西小学附近推荐榜Top10 - 工业品网
  • 小白救星!更贴合研究生的降AIGC网站,千笔·专业降AIGC智能体 VS 云笔AI
  • 2026年注浆水玻璃厂家十大排名,可靠的水玻璃厂商分享 - mypinpai
  • 2026年上海靠谱校服定制公司推荐,爱布谷校服舒适度如何 - myqiye
  • 算法练习刷题题单 | 基础算法(866题)
  • 聊聊2026年上海地区口碑好的乘务学校,上海万通教育值得选吗 - 工业设备
  • 2026年服饰配饰专业板扣五金源头厂商排名揭晓 - 工业品网
  • 信创系统下PHP实现500M大文件上传的解决方案?
  • 分析市政管道高压清淤疏通,口碑好的公司有哪些 - 工业设备
  • 2026年半抛坯板扣五金厂家排名,哪家值得选 - 工业品网
  • 当我把飞算JavaAI专业版当同事后,加班焦虑少了一半
  • 毕业论文救星!2026 AI 写论文软件 TOP 榜(高性价比 + 避坑指南)
  • 2026最新!降AIGC网站 千笔·专业降AI率智能体 VS PaperRed,研究生专属降重神器!
  • AI 写论文软件哪家强?2026 高性价比款直接冲
  • 专科生也能用!万众偏爱的AI论文网站 —— 千笔·专业论文写作工具
  • 少走弯路:专科生专属AI论文网站 —— 千笔AI
  • 如何为不同场景选择眼液产品?2026年眼疲劳眼液推荐与评测,直击安全与功效痛点 - 品牌推荐
  • 应用安全 --- IDAPro问题 之 为什么记载的模块地址一直不变或者定位到相对地址后发现指令不对