news 2026/3/10 20:33:58

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/3/10 20:45:28

【开题答辩全过程】以 基于SSM的房产中介信息系统为例,包含答辩的问题和答案

个人简介一名14年经验的资深毕设内行人&#xff0c;语言擅长Java、php、微信小程序、Python、Golang、安卓Android等开发项目包括大数据、深度学习、网站、小程序、安卓、算法。平常会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。感谢大家的…

作者头像 李华
网站建设 2026/3/10 10:28:54

百泰派克生物科技:蛋白质表征客户案例(一)

百泰派克生物科技&#xff1a;蛋白质表征客户案例&#xff08;一&#xff09;期刊&#xff1a;LWT Food Science and Technology&#xff1b;影响因子&#xff1a;6.2947原文链接&#xff1a;DOI: 10.1016/j.lwt.2021.111456研究对象&#xff1a;蓝莓细菌素JS17研究目的&#x…

作者头像 李华
网站建设 2026/3/10 8:35:02

【开题答辩全过程】以 基于SSM的好物推荐系统的设计与实现为例,包含答辩的问题和答案

个人简介一名14年经验的资深毕设内行人&#xff0c;语言擅长Java、php、微信小程序、Python、Golang、安卓Android等开发项目包括大数据、深度学习、网站、小程序、安卓、算法。平常会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。感谢大家的…

作者头像 李华
网站建设 2026/3/3 22:52:06

GESP2025年6月认证C++二级( 第三部分编程题 1、数三角形)

《数三角形的秘密》&#x1f4d6; 一、故事背景&#xff1a;三角形王国 &#x1f3f0;1、在 三角形王国 里&#xff0c;有一种特别的三角形&#xff0c;叫做&#xff1a;直角三角形2、它有&#xff1a;两条直角边&#xff08;互相垂直 &#x1f4d0;&#xff09;一条斜边3、国王…

作者头像 李华
网站建设 2026/3/7 7:05:12

No.1186 S7-200 PLC与用组态王实现锅炉水温串级调节系统的

No.1186 S7-200 PLC和用组态王实现锅炉水温串级调节系统的锅炉房里冒着热气的老设备突然开始抖动了&#xff1f;控制面板上的温度指针来回晃得人心慌&#xff1f;今天咱们来盘一套能让锅炉水温稳如老狗的串级控制系统&#xff0c;用S7-200 PLC搭框架&#xff0c;组态王做监控&a…

作者头像 李华
网站建设 2026/3/9 20:10:54

专注力掌控神器:在线番茄时钟 Focus Timer

在信息爆炸、干扰不断的数字时代&#xff0c;专注已成为最宝贵的生产力资源。我们很高兴向您介绍 Focus Timer——一款基于经典番茄工作法的在线专注力管理工具&#xff0c;帮助您将时间分割成高效的工作单元&#xff0c;最大化工作效率。 工具概览 Focus Timer 是一个简洁而…

作者头像 李华