前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >面试官:SpringBoot 启动时如何自动执行代码?

面试官:SpringBoot 启动时如何自动执行代码?

作者头像
JavaFish
发布2022-03-15 13:48:37
4350
发布2022-03-15 13:48:37
举报

01 前言

哈喽,我是狗哥,在日常开发中,SpringBoot项目在启动的时候需要预加载一些资源。而如何实现启动过程中执行代码,或启动成功后执行,是有很多种方式可以选择,我们可以在static代码块中实现,也可以在构造方法里实现,还可以使用@PostConstruct注解实现。

当然也可以去实现SpringApplicationRunnerCommandLineRunner接口去实现启动后运行的功能。在这里整理一下,在这些位置执行的区别以及加载顺序。

02 java 自身的启动时加载方式

2.1 static代码块

static 静态代码块,在类加载的时候即自动执行。

2.2 构造方法

在对象初始化时执行。执行顺序在 static 静态代码块之后。

03 Spring 启动时加载方式

3.1 @PostConstruct注解

PostConstruct注解使用在方法上,这个方法在对象依赖注入初始化之后执行。

3.2 ApplicationRunner 和 CommandLineRunner

SpringBoot 提供了两个接口来实现Spring容器启动完成后执行的功能,两个接口分别为CommandLineRunnerApplicationRunner

这两个接口需要实现一个run方法,将代码在run中实现即可。这两个接口功能基本一致,其区别在于run方法的入参。ApplicationRunner的run方法入参为ApplicationArguments,为CommandLineRunnerrun方法入参为String数组。

3.3 什么是 ApplicationArguments?

官方文档解释为:

Provides access to the arguments that were used to run a SpringApplication.

Spring应用运行时使用的访问应用参数。即我们可以获取到SpringApplication.run(…)的应用参数。

3.4 Order注解

当有多个类实现了CommandLineRunnerApplicationRunner接口时,可以通过在类上添加@Order注解来设定运行顺序。

3.5 代码测试

为了测试启动时运行的效果和顺序,编写几个测试代码来运行看看。

TestPostConstruct

代码语言:javascript
复制
@Component
public class TestPostConstruct {

    static {
        System.out.println("static");
    }
    public TestPostConstruct() {
        System.out.println("constructer");
    }

    @PostConstruct
    public void init() {
        System.out.println("PostConstruct");
    }
}

TestApplicationRunner

代码语言:javascript
复制
@Component
@Order(1)
public class TestApplicationRunner implements ApplicationRunner{
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("order1:TestApplicationRunner");
    }
}

TestCommandLineRunner

代码语言:javascript
复制
@Component
@Order(2)
public class TestCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        System.out.println("order2:TestCommandLineRunner");
    }
}

执行结果

04 总结

Spring应用启动过程中,肯定是要自动扫描有@Component注解的类,加载类并初始化对象进行自动注入。加载类时首先要执行static静态代码块中的代码,之后再初始化对象时会执行构造方法。

在对象注入完成后,调用带有@PostConstruct注解的方法。当容器启动成功后,再根据@Order注解的顺序调用CommandLineRunnerApplicationRunner接口类中的run方法。

因此,加载顺序为static>constructer>@PostConstruct>CommandLineRunnerApplicationRunner.

05 参考链接

  • blog.csdn.net/u011291072/article/ details/81813662
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 一个优秀的废人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01 前言
  • 02 java 自身的启动时加载方式
    • 2.1 static代码块
      • 2.2 构造方法
      • 03 Spring 启动时加载方式
        • 3.1 @PostConstruct注解
          • 3.2 ApplicationRunner 和 CommandLineRunner
            • 3.3 什么是 ApplicationArguments?
              • 3.4 Order注解
                • 3.5 代码测试
                • 04 总结
                • 05 参考链接
                相关产品与服务
                容器服务
                腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档