• SpringBoot启动后执行方法的几种方式


    1 注解@PostConstruct

    使用注解**@PostConstruct是最常见的一种方式,不过该注解生命周期,项目还未初始化完毕。故存在的问题:如果执行的方法耗时过长**,会导致项目在方法执行期间无法提供服务。

    @Component
    @Slf4j
    public class PostConstructInit {
        
        @PostConstruct
        public void init() {
            try {
                Thread.sleep(10 * 1000);
                log.info("postConstruct执行结束...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    

    2 实现CommandLineRunner接口

    实现CommandLineRunner接口,然后在run方法里面调用需要调用的方法即可,好处是方法执行时,项目已经初始化完毕,是可以正常提供服务的。同时该方法也可以接受参数,可以根据项目启动时: java -jar app.jar 参数1 参数2 参数3... 将参数传入进行一些处理。

    @Component
    @Slf4j
    public class CommandLineRunnerInit implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            log.info("commandLineRunner args:{}", args);
        }
    }
    

    3 实现ApplicationRunner接口

    实现ApplicationRunner接口的方式和实现CommandLineRunner接口基本相同。一般来说,实现ApplicationRunner接口的run方法启动时先执行,而实现CommanLineRunner接口的方法后执行。但是,如果我们想要指定启动方法的执行顺序,可以使用 @Order注解,或者实现 Ordered 接口来指定。除此以外,启动时传参的格式也略显不同,CommandLineRunner对于参数格式没有任何限制,ApplicationRunner接口参数格式必须是:–key=value

    @Component
    @Slf4j
    public class ApplicationRunnerInit implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            log.info("applicationRunner args: {}", args);
        }
    }
    

    4 实现ApplicationListener

    实现接口ApplicationListener方式和实现ApplicationRunner,CommandLineRunner接口都不影响服务,都可以正常提供服务,注意监听的事件,通常是ApplicationStartedEvent 或者ApplicationReadyEvent,其他的事件可能无法注入bean。

    @Component
    @Slf4j
    public class ApplicationListenerInit implements ApplicationListener<ApplicationStartedEvent> {
        @Override
        public void onApplicationEvent(ApplicationStartedEvent event) {
            log.info("applicationListener event:{}", event.toString());
        }
    }
    

    对比这四种SpringBoot启动后的执行方法,在不进行配置的情况下,执行的先后顺序为:

    在这里插入图片描述

  • 相关阅读:
    Unity UGUI的Outline(描边)组件的介绍及使用
    Mysql主从复制
    2020架构真题(四十六)
    C++前缀和算法的应用:统计得分小于K的子数组数目
    VuePress + Github Pages 搭建博客网站
    C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
    Python+Requests+Pytest+Excel+Allure 接口自动化测试项目实战【框架之间的对比】
    java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 异常错误
    Vue学习笔记四
    Naopore基因组数据组装软件---NextDenovo下载试用
  • 原文地址:https://blog.csdn.net/SmallPig_Code/article/details/127095544