当前位置: 首页 > news >正文

详谈SpringBoot启动项目后执行自定义方法的方式

在 main 启动函数中调用

这个是在所有启动后执行,也是常用之一。

@SpringBootApplication
public class ListenerApplication {public static void main(String[] args) {SpringApplication.run(ListenerApplication.class, args);System.out.println("启动成功");}
}

实现 CommandLineRunner 接口

项目初始化完毕后才会调用方法,提供服务。好处是方法执行时,项目已经初始化完毕,是可以正常提供服务的。

@Component
public class CommandLineRunnerImpl implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println(Arrays.toString(args));}
}

实现 ApplicationRunner 接口

实现 ApplicationRunner 接口和实现 CommandLineRunner 接口基本是一样的。不同的是启动时传参的格式,CommandLineRunner 对于参数格式没有任何限制。

ApplicationRunner 接口参数格式必须是:key=value

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {Set<String> optionNames = args.getOptionNames();for (String optionName : optionNames) {List<String> values = args.getOptionValues(optionName);System.out.println(values.toString());}}
}

注意

CommandLineRunner 和 ApplicationRunner 默认是 ApplicationRunner 先执行,如果双方指定了@Order 则按照 @Order 的大小顺序执行,大的先执行。

实现 ApplicationListener 接口

实现 ApplicationListener 接口和实现 ApplicationRunner、CommandLineRunner 接口基本差不多,项目初始化完毕后才会调用方法,提供服务。

@Component
public class ApplicationListenerImpl implements ApplicationListener<ApplicationStartedEvent> {@Overridepublic void onApplicationEvent(ApplicationStartedEvent event) {System.out.println("ApplicationStartedEvent");}}

当以 @Component 方式配置时,事件触发顺序如下:

ApplicationListener<ServletWebServerInitializedEvent>
ApplicationListener<ContextRefreshedEvent>
ApplicationListener<ApplicationStartedEvent>
ApplicationListener<ApplicationReadyEvent>
ApplicationListener<ContextClosedEvent>

当通过 /META-INF/spring.factories 配置时,事件触发顺序如下:

org.springframework.context.ApplicationListener=com.xh.event.ApplicationListenerImplApplicationListener<ApplicationStartingEvent>
ApplicationListener<ApplicationEnvironmentPreparedEvent>
ApplicationListener<ApplicationContextInitializedEvent>
ApplicationListener<ApplicationPreparedEvent>
ApplicationListener<ServletWebServerInitializedEvent>
ApplicationListener<ContextRefreshedEvent>
ApplicationListener<ApplicationStartedEvent>
ApplicationListener<ApplicationReadyEvent>
ApplicationListener<ContextClosedEvent>

注意 

  • 如果监听的是 ServletWebServerInitializedEvent、ContextRefreshedEvent、ApplicationStartedEvent 事件,则 ApplicationListener 会在 CommandLineRunner 和 ApplicationRunner 之前执行
  • 如果监听的是 ApplicationReadyEvent 事件,则 ApplicationListener 会在 CommandLineRunner 和 ApplicationRunner 之后执行
  • ContextClosedEvent 当调用关闭方法的时候,才会触发了 ContextClosedEvent 事件

实现 InitializingBean 接口

项目启动时,调用此方法,在 ServletWebServerInitializedEvent 事件前触发。

@Component
public class InitializingExample implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("InitializingExample");}
}

@PostConstruct 注解

使用注解 @PostConstruct 实现,不推荐使用。如果执行的方法耗时过长,会导致服务无法提供服务。

@Component
public class StartBoot {@PostConstructpublic void init() throws InterruptedException {System.out.println("@PostConstruct");}
}
http://www.lryc.cn/news/150686.html

相关文章:

  • KubeAdmin方式搭建K8S(1.26.0)
  • 代码随想录打卡—day57—【编辑距离】— 9.2+9.3 编辑距离系列
  • Blender界面学习03 原点、鼠标所在位置的缩放与旋转
  • 指针结构体题
  • 【力扣每日一题02】数组篇--删除有序数组中的重复项
  • Vue在表格中拿到该行信息的方式(作用域插槽-#default-scope-解决按钮与行点击的顺序问题)
  • OJ练习第158题——单词拆分 II
  • ArcGIS地块面积分割调整工具插件
  • 基于Matlab实现多个图像增强案例(附上源码+数据集)
  • 计算机网络 概述部分
  • 使用DOSBOX运行TurboC2,TC2使用graphics库绘图
  • OpenCV(二):认识Mat容器
  • springboot整合Excel填充数据
  • c语言技术面试记录 ---- 纲要、题目、分析及给分标准
  • 前端进阶之——模块化
  • Python爬虫抓取表情包制作个性化聊天机器人
  • 使用pip命令安装库,装到其他环境中的问题。
  • 如何使用CSS实现一个带有动画效果的进度条?
  • uni-app 报错 navigateTo:fail page “/pages/.../...“ is not found
  • 【unity插件】使用BehaviorDesigner插件制作BOSS的AI行为树
  • 概念解析 | 量子机器学习:将量子力学与人工智能的奇妙融合
  • 【Cortex-M3权威指南】学习笔记4 - 异常
  • RISC-V(2)——特权级及特权指令集
  • Linux——常用命令大汇总(带你快速入门Linux)
  • 记录 使用 git 克隆仓库报错:Warning: Permanently added‘github.com’ to the .....(ssh )
  • kafka---- zookeeper集群搭建
  • linux安装firefox
  • 【MySQL】基础语法总结
  • 【玩玩Vue】使用el-menu作为菜单时,通过一二级路由控制菜单高亮
  • 9.2 【C语言】使用结构体数组