首页 > 编程语言 > 详细

想springboot启动完成后执行某个方法

时间:2020-07-15 17:43:42      阅读:47      评论:0      收藏:0      [点我收藏+]

如题,很多时候,我们都需要在springboot项目启动后初始化化一些自己的数据

原文地址:https://www.jianshu.com/p/f80f833ab8f6
实现方法有2个。
一、ApplicationRunner
实现ApplicationRunner接口
打上@Component+implements ApplicationRunner

@Component
public class DemoApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner");
    }
}

二、CommandLineRunner
实现CommandLineRunner接口
打上@Component+implements CommandLineRunner

@Component
public class DemoComLiner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner");
    }
}

原理讲解

SpringApplication的run方法会执行afterRefresh方法
技术分享图片

afterRefresh会触发callRunners方法
技术分享图片

callRunners方法会调用容器里面所有实现了ApplicationRunner、CommandLineRunner接口的方法
技术分享图片

想springboot启动完成后执行某个方法

原文:https://www.cnblogs.com/yeyongjian/p/13306334.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!