springboot项目启动后执行方法,有三种实现方式。

1 方法

  • ApplicationListener< ContextRefreshedEvent> 不推荐
  • ApplicationListener  推荐
  • CommandLineRunner 推荐

方法1:spring的ApplicationListener< ContextRefreshedEvent>接口

实现ApplicationListener接口,并实现 onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent)方法

@Service
public class SearchReceive implements  ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次//需要执行的方法}}
}

方法2:springboot的ApplicationRunner接口

ApplicationListener和CommandLineRunner两个接口是springBoot提供用来在spring容器加载完成后执行指定方法。两个接口区别主要是入参不同。

实现ApplicationRunner接口

@Component
@Order(value = 1)
public class AfterRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("执行方法");}
}

方法3:springboot的CommandLineRunner接口

实现CommandLineRunner接口

@Component
@Order(value = 2)
public class CommandLineRunnerImpl implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("执行方法");}
}

注:如果同时implements   ApplicationListener和CommandLineRunner两个接口,ApplicationRunner接口的方法先执行,CommandLineRunner后执行;


@Slf4j
@Component
public class RunnerTest implements ApplicationRunner, CommandLineRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("服务启动RunnerTest   ApplicationRunner执行启动加载任务...");}@Overridepublic void run(String... args) throws Exception {System.out.println("服务启动RunnerTest    CommandLineRunner 执行启动加载任务...");}}
}

2 指定执行顺序

当项目中同时实现了ApplicationRunner和CommondLineRunner接口时,可使用Order注解或实现Ordered接口来指定执行顺序,值越小越先执行。

3 原理

SpringApplication 的run方法会执行afterRefresh方法。
        afterRefresh方法会执行callRunners方法。
        callRunners方法会调用所有实现ApplicationRunner和CommondLineRunner接口的方法。

springboot项目启动后执行方法相关推荐

  1. Springboot 项目启动后执行某些自定义代码

    Springboot 项目启动后执行某些自定义代码 Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRun ...

  2. Springboot项目启动后立即执行方法

    Springboot项目启动后执行方法,有三种实现方式. 此篇博客介绍的方法可以在程序启动时加载一些自定义的监听器之类的,例如Socket服务的监听器,此时如果使用@PostConstract,Soc ...

  3. 为什么当项目启动后执行两次过滤器 再进行访问资源时执行一次过滤器

    为什么当项目启动后执行两次过滤器 再进行访问资源时执行一次过滤器 粗线的理解,启动项目后进行了两次请求,访问两次过滤器.访问资源时只进行一次请求,所以访问一次过滤器. 于是为了更加清楚地看清在启动项目 ...

  4. Springboot项目启动后自动在浏览器打开

    Springboot项目启动后自动在浏览器打开(超简单)_杨默他的国的博客-CSDN博客_springboot启动后怎么看网页 try {InetAddress addr = InetAddress. ...

  5. Springboot项目启动后自动打开浏览器访问(超实用)

    Springboot项目启动后自动打开浏览器访问 1.在Springboot项目中每次启动完项目,手动在浏览器输入访问地址太麻烦了.在启动类中加入下方代码,就可高效地在控制台中单击URL访问项目了~ ...

  6. Springboot项目启动前执行数据库初始化脚本

    背景:项目里面遇到了要在springboot项目启动前做数据库初始化的需求.总结一下几种方案: 1.使用flywaydb,启动工程的时候同时初始化脚本.集成倒是不难,主要是要解决bean的顺序加载问题 ...

  7. springboot2.X 在项目启动后执行一段自定义代码

    场景: 项目需要在项目启动后从数据库初始化一些数据进入redis , 但是没有很适合 的监听器去实现 , 监听 老是在dao初始化之前触发. 解决方法:自定义类实现 ApplicationRunner ...

  8. springBoot项目启动后无法访问index.html首页或其它controller

    springBoot项目上手和启动比较简单,但也还是会遇到不少细节问题. 无法访问被@RestController标注的控制器对应的@RequestMapping映射地址 首先重点考虑访问地址是否准确 ...

  9. 新建springboot项目启动后自动停止

    问题描述 刚开始学习SpringBoot,新建项目启动后自动停止. 2021-04-26 10:10:22.123 INFO 18992 --- [ main] com.fanfan.Springbo ...

  10. JAVA - 项目启动后执行定时任务

    项目启动后不执行定时任务?? 这个得在启动类上加 @EnableScheduling 注解 @EnableScheduling @SpringBootApplication public class ...

最新文章

  1. (转载)不一样的冒泡排序
  2. Python基于python实现的http+json协议接口自动化测试框架源码(实用改进版)
  3. SQL:数据表给定字段的多行记录的合并
  4. 树莓派IO口驱动代码的编写、微机总线地址、物理地址、虚拟地址、BCM2835芯片手册
  5. Uniapp学习笔记(数据展示、数据循环、条件编译、计算属性、组件的使用、组件插槽、生命周期)
  6. 【原创】大叔经验分享(6)Oozie如何查看提交到Yarn上的任务日志
  7. Eclipse JaveEE版本
  8. Linux(12.1-12.6)学习笔记
  9. Node.js .sh scripts Manager
  10. j2me怎么运行java_[转载]使用J2ME进行开发一个简单的KJava应用程序示例(2)
  11. oracle数据库课后报告,ORACLE数据库课程设计报告
  12. 思科CCNP在OSPF路由协议中的五类LSA是什么
  13. 分布式技术(6) 后端接口设计方案
  14. 倪光南华为鸿蒙,倪光南谈华为鸿蒙:国产鸿蒙操作系统需要生态支持
  15. 一文解决关于建立时间和保持时间的困惑
  16. 关于物联网远程控制技术,你了解的有多少?
  17. vscode 终端运行yarn 报错 “因为在此系统上禁止运行脚本”
  18. Unity基础篇:Serializable总结与深入研究。
  19. thinkphp查询条件被叠加
  20. 808操作系统 设备管理

热门文章

  1. 用dw做html表格边框,Dreamweaver怎么设计彩色边框的表格?
  2. 地址规范化--城市三级联动(layui) (B)
  3. JumpServer页面访问502、504问题记录
  4. 极客时间和极客学院_本周极客历史:旅行者指南,光盘和旋风式操作系统
  5. ios15验证失败因为你不在连接互联网
  6. 2021第一届网刃杯网络安全大赛-签到题
  7. ensp服务器配置文件,ensp配置web服务器
  8. Ubuntu20.04安装过程 【磁盘分区】
  9. uni-app实现APP中打开第三方app
  10. 多功能计算机器在线,多功能数学计算器(RedCrab The Calculator)