news 2026/4/16 11:09:46

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/4/14 20:27:53

少走弯路:专科生专属AI论文网站 —— 千笔AI

你是否曾为论文选题发愁&#xff0c;反复修改却总对表达不满意&#xff1f;是否在查重率和格式问题上焦头烂额&#xff1f;专科生的论文之路本就充满挑战&#xff0c;而千笔AI正是为你们量身打造的智能写作助手。它用人工智能技术解决选题、框架、查重、格式等全流程难题&#…

作者头像 李华
网站建设 2026/4/15 3:26:12

【开题答辩全过程】以 基于SSM的共享自习室预约管理系统的设计与实现为例,包含答辩的问题和答案

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

作者头像 李华
网站建设 2026/4/16 2:47:34

与AI聊天机器人沟通的最佳方式:使用正式语言

与AI聊天机器人沟通的最佳方式&#xff1a;使用正式语言 你与AI聊天机器人交流时是否简短且随意&#xff1f;如果是这样&#xff0c;你得到的答案可能比使用更正式语言时更差。 一项研究表明&#xff0c;像许多人那样用不太正式的语言与AI聊天机器人交谈&#xff0c;会降低其回…

作者头像 李华
网站建设 2026/4/14 23:36:48

第 488 场周赛Q1——100985. 统计主导元素下标数

题目链接&#xff1a;100985. 统计主导元素下标数&#xff08;简单&#xff09; 算法原理&#xff1a; 解法&#xff1a;前缀和 1ms击败100.00% 时间复杂度O(N) 思路很简单&#xff0c;既然主导元素是看当前元素是否>后面所有数的平均数&#xff0c;那么我们只需要在遍历每个…

作者头像 李华