zoukankan      html  css  js  c++  java
  • 想springboot启动完成后执行某个方法

    如题,很多时候,我们都需要在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方法
    SpringApplication

    afterRefresh会触发callRunners方法
    afterRefresh

    callRunners方法会调用容器里面所有实现了ApplicationRunner、CommandLineRunner接口的方法
    callRunners

  • 相关阅读:
    模拟
    广搜——最优方案
    动态规划——背包
    动态规划——树规
    动态规划——区间
    fill 的用法
    状态压缩dp
    超大背包问题
    lower_bound
    弹性碰撞 poj 3684
  • 原文地址:https://www.cnblogs.com/yeyongjian/p/13306334.html
Copyright © 2011-2022 走看看