SpringBoot启动时行初始化方法
本文共 2,963 字,预计阅读时间 10 分钟
有时需要在SpringBoot启动时进行一些初始化的操作,有以下几种方法:
1.实现CommandLineRunner接口
在启动类上实现CommandLineRunner接口,重写run方法
package com.zxh; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @Slf4j public class GatewayApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Value("${server.port}") private String port; @Override public void run(String... args) throws Exception { log.info("系统初始化中。。。。。"); log.info("端口号: {}", port); } }
2.使用注解@PostConstruct
新建一个配置类,在要进行初始化的方法上加注解@PostConstruct:
package com.zxh.config; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; /** * 系统初始化操作 */ @Configuration @Slf4j public class WebAppConfig { @Value("${server.port}") private String port; @PostConstruct public void initRun() { log.info("系统初始化中。。。。。"); log.info("端口号: {}", port); } }
3.使用注解@EventListener
package com.zxh.config; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; /** * 系统初始化操作 */ @Configuration @Slf4j public class WebAppConfig { @Value("${server.port}") private String port; @EventListener public void initRun(ContextRefreshedEvent event) { log.info("系统初始化中。。。。。"); log.info("端口号: {}", port); } }
4.实现ApplicationRunner接口
新建类,实现ApplicationRunner接口重写run方法,并将其交给Spring管理
package com.zxh.test.config; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component @Slf4j public class WebApplicationConfig implements ApplicationRunner { @Value("${server.port}") private String port; @Override public void run(ApplicationArguments args) throws Exception { log.info("系统初始化中。。。。。"); log.info("端口号: {}", port); } }
对比发现,CommandLineRunner和ApplicationRunner都是实现接口,重写其run方法,它们的作用是相同的,但也有一定的区别。主要区别就在于其run方法中的参数,ApplicationRunner中run方法的参数是ApplicationArguments,而CommandLineRunner中run方法的参数是数组,这些参数都是启动SpringBoot程序时传给main()方法的。ApplicationArguments是对main方的参数做了进一步的封装。
上述的几种方法任选一种即可。
就是这么简单,你学废了吗?感觉有用的话,给笔者点个赞吧 !
分类:
03-SpringBoot相关
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用perf跟踪.NET程序的mmap泄露
· 日常问题排查-空闲一段时间再请求就超时
· Java虚拟机代码是如何一步一步变复杂且难以理解的?
· 领域驱动的事实与谬误 一 DDD 与 MVC
· SQL Server 2025 中的改进
· C# 14 新增功能一览,你觉得实用吗?
· C#/.NET/.NET Core优秀项目和框架2025年4月简报
· Linux系列:如何用perf跟踪.NET程序的mmap泄露
· .NET + AI | Semantic Kernel vs Microsoft.Extension
· windows11 安装WSL2详细过程
2020-09-09 React高级