• springboot项目启动后执行方法


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

    1 方法

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

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

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

    @Service
    public class SearchReceive implements  ApplicationListener {
       @Override
       public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
           if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次
               //需要执行的方法
           }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    方法2:springboot的ApplicationRunner接口

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

    实现ApplicationRunner接口

    @Component
    @Order(value = 1)
    public class AfterRunner implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("执行方法");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    方法3:springboot的CommandLineRunner接口

    实现CommandLineRunner接口

    @Component
    @Order(value = 2)
    public class CommandLineRunnerImpl implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("执行方法");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

    @Slf4j
    @Component
    public class RunnerTest implements ApplicationRunner, CommandLineRunner {
    
      @Override
      public void run(ApplicationArguments args) throws Exception {
        System.out.println("服务启动RunnerTest   ApplicationRunner执行启动加载任务...");
      }
    
      @Override
      public void run(String... args) throws Exception {
        System.out.println("服务启动RunnerTest    CommandLineRunner 执行启动加载任务...");
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2 指定执行顺序

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

    3 原理

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

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    信息学奥赛一本通:1154:亲和数
    计数器介绍
    开源.NetCore通用工具库Xmtool使用连载 - 图形验证码篇
    【Linux】(四)VS Code远程开发方式-实验室服务器使用VS Code远程开发
    什么是Jmeter?Jmeter使用的原理步骤是什么?
    HTML标签全解
    Python按单元格读取复杂电子表格(Excel)数据实践
    实验4 图像空间滤波
    uniapp|微信小程序获取当前城市名称--逆地址解析
    【毕业设计】深度学习OCR中文识别系统 - opencv python
  • 原文地址:https://blog.csdn.net/m0_67392126/article/details/126115918