• 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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    【BOOST C++ 13 并行编程】(5) Boost.Atomic
    曝光网上流传的新白薅某宝虚拟物品正规国内educn邮箱方法0风险成功率100%(附带话术
    2024深圳电子展,推动中国电子产业快速发展,助力中国数字经济实现高质量发展
    直播预约丨《实时湖仓实践五讲》第三讲:实时湖仓在袋鼠云的落地实践之路
    【CSDN技术】Markdown编辑器如何使用-csdn博客编写入门
    地震数据处理研究(Matlab代码实现)
    大学校园广播“录编播”与IP校园公共广播系统技术方案
    蓝桥杯-修建灌木
    法国A+ 法国VOC标签最高环保级别
    计算机毕业设计Java茶叶企业管理系统(源码+系统+mysql数据库+lw文档)
  • 原文地址:https://blog.csdn.net/m0_67392126/article/details/126115918