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

此篇博客介绍的方法可以在程序启动时加载一些自定义的监听器之类的,例如Socket服务的监听器,此时如果使用@PostConstract,Socket服务的监听器将阻塞启动程序,导致程序不能正常启动。

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接口

ApplicationRunner和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项目启动后自动在浏览器打开(超简单)_杨默他的国的博客-CSDN博客_springboot启动后怎么看网页 try {InetAddress addr = InetAddress. ...

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

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

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

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

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

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

  6. SpringBoot中实现CommandLineRunner接口在项目启动后立即执行某方法

    场景 在启动SpringBoot项目的启动类之后需要其立即执行某方法. 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取 ...

  7. springboot项目启动后找不到jsp文件

    1.场景 项目是一个maven搭建的springboot项目,然后启动后,一起正常,就是到最后访问jsp页面的时候,就报404找不到资源. 2.解决方法 或者是添加相对路径:$MODULE_DIR$

  8. springboot项目启动后无法访问index.html首页

    今天新建了一个springboot项目,目录如下: index.html在static目录下. 然后启动后访问端口仍然显示error page,找了一下看到有一个说是要加上themleaf依赖: &l ...

  9. 刚创建的springboot项目启动后访问controll报404

    访问controll报404的原因之一,可能是springboot的启动类位置不对. 这种情况需要把启动类放到与controller文件夹同级.为了方便查看先改变包结构. 选择package Pres ...

最新文章

  1. SD--关于定价过程中的存储顺序的参考结构和参考字段的使用代码跟踪
  2. [Python学习] 专题二.条件语句和循环语句的基础知识
  3. 工作中让你提建议怎么办?
  4. leetcode 684. 冗余连接()
  5. 炒股如果亏损一直买入可行吗
  6. ListView.setOnItemClickListener 点击无效
  7. Android Studio 设置/更改 SDK 路径
  8. hadoop官方hdfs操作说明
  9. 不要着急改代码,先想想--centos 6.8下编译安装tmux
  10. 【备忘】Python编程实践视频教程+教材 27集 下载
  11. winpe加载raid_winpe里添加raid驱动
  12. 免费开源的电路图和PCB绘图软件KiCAD
  13. JAVA一键获取文件名并对比Excel名单,看看谁没有交作业
  14. 第三章——Lyapunov理论基础
  15. html调色板快捷键,在线网页调色板
  16. Qt编写1024游戏
  17. mysql获取今天的最后一秒_Oracle取得当天0时0分0秒和23时59分59秒
  18. 养猫有哪些坏处哪些好处呢?
  19. 电视html转vga没有声音,手把手教你排除HDMI转VGA常见故障
  20. was not declared in this scop

热门文章

  1. c语言求a b 最大公约数和最小公倍数,常见算法:C语言求最小公倍数和最大公约数三种算法...
  2. 在美团外卖APP里,你现在可以点一份海澜之家
  3. 转换阿拉伯数字为人名币大写
  4. android:ArrayList<String>转数组String[]
  5. java alertdialog_Android AlertDialog始终位于顶部
  6. Poi模板导出(设置字体样式)
  7. php中树形结构转数组(拉平树结构,保留上下级排序)
  8. Java代码模拟Redis
  9. CDH6.3.2 Hive安装
  10. 电脑无法显示计算机,解决方案:如果无法打开计算机且屏幕不显示,该怎么办?台式电脑开机黑屏修复技巧...