• 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启动后的执行方法,在不进行配置的情况下,执行的先后顺序为:

    在这里插入图片描述

  • 相关阅读:
    【考研】数据结构考点——快速排序(重点,含408真题)
    做技术开发到老 or 晋升管理层,程序员的终极目标是什么?
    数据结构和算法(13):优先级队列
    Windows 11 上使用安卓应用及安装谷歌 Google Play和亚马逊 应用商店
    代码随想录算法训练营day30||332.重新安排行程 ||第51题. N皇后||37. 解数独
    webrtc Native C++ 客户端的内存释放问题
    Sqoop
    ZEMAX | 如何创建复杂的非序列物体
    html 生成1-100的随机数
    DELETE和TRUNCATE的区别
  • 原文地址:https://blog.csdn.net/SmallPig_Code/article/details/127095544