news 2026/6/23 15:23:07

springboot 启动时就执行特定接口

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/14 0:36:21

专科生也能用!万众偏爱的AI论文网站 —— 千笔·专业论文写作工具

你是否曾为论文选题而发愁&#xff1f;是否在深夜面对空白文档无从下笔&#xff1f;是否反复修改却总对表达不满意&#xff1f;对于自考学生来说&#xff0c;论文写作不仅是一项学术任务&#xff0c;更是一场与时间的较量。选题难、框架乱、查重高、格式错……这些问题让无数人…

作者头像 李华
网站建设 2026/6/23 14:02:43

解决 VS Code Claude Code 插件「Allow this bash command_」弹窗问题

解决 VS Code Claude Code 插件「Allow this bash command?」弹窗问题 本文针对 VS Code 中使用 Claude Code 插件时&#xff0c;每次执行任务&#xff08;如代码生成、文件分析、命令调用&#xff09;均弹出「Allow this bash command?」&#xff08;或对应终端类型的授权提…

作者头像 李华
网站建设 2026/6/19 19:04:40

黑马大模型RAG与Agent智能体实战教程LangChain提示词——5、提示词工程(Json数据格式、json.dumps()、json.loads()、ensure_ascii=False)

https://www.bilibili.com/video/BV1yjz5BLEoY https://hzh.sealos.run/ 文章目录提示词工程-04、Json数据格式Json结构介绍→提示词&#xff1a;帮我创建第六个代码&#xff0c;根据图中内容&#xff0c;演示在python中使用json←AI回复06_JSON_Usage_Demo.py运行测试总结提示…

作者头像 李华
网站建设 2026/6/22 22:58:03

修正的Butler-Volmer方程

comsol多束锂枝晶生长模型。锂金属阳极表面冒出来的枝晶像一群不安分的触手&#xff0c;搞起破坏来比熊孩子拆家还狠。我在实验室里用COMSOL折腾多束枝晶模型的时候&#xff0c;发现这玩意儿比煮糊的意大利面还难预测——你永远不知道下一根枝晶会在哪个方向突然支棱起来。先甩…

作者头像 李华
网站建设 2026/6/19 19:59:23

2026年十大最美Linux发行版,每个都美到上瘾,且稳定强大

后台很多粉丝问:“我想换Linux,但要好看、不折腾,有哪些发行版推荐?”今天这篇超干货,就为大家盘点2026年十大最美Linux发行版。这些不是随便换壁纸,而是深度美化桌面、图标、动画,全开箱即用,颜值直接天花板! 这些发行版大多基于Ubuntu/Debian/Arch,稳定+最新软件。…

作者头像 李华