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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2020-09-09 React高级