springboot 启动时就执行特定接口

📅 发布时间:2026/7/7 2:05:57 👁️ 浏览次数:
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() { // 接口调用逻辑 } } 方法三PostConstructBean 初始化后执行适用于某个特定 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 ApplicationListenerContextRefreshedEvent { 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; ResponseEntityString 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