Spring Boot项目启动时初始化资源

在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。CommandLineRunner就是专门解决项目启动初始化资源操作。

CommandLineRunner基本使用

CommandLineRunner接口的Component会在所有Spring Beans都初始化之后,SpringApplication.run()之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作。

接下来我们就运用案例测试它如何使用,在测试之前在启动类加两行打印提示,方便我们识别CommandLineRunner的执行时机。

@SpringBootApplication
public class CommandLineRunnerApplication {
    public static void main(String[] args) {
        System.out.println("The service to start.");
        SpringApplication.run(CommandLineRunnerApplication.class, args);
        System.out.println("The service has started.");
    }
}

接下来我们直接创建一个类继承CommandLineRunner,并实现它的run()方法

@Component
public class Runner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The Runner start to initialize ...");
    }
}

run()方法中打印了一些参数来看出它的执行时机。完成之后启动项目进行测试:

The service to start.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

...

Tomcat started on port(s): 8080 (http) with context path ''
Started CommandLineRunnerApplication in 2.952 seconds (JVM running for 4.556)
The Runner start to initialize ...
The service has started.

根据控制台的打印信息我们可以看出CommandLineRunner中的方法会在 Spring Boot 容器加载之后执行,执行完成后项目启动完成。

CommandLineRunner执行顺序

如果我们在启动容器的时候需要初始化很多资源,并且初始化资源相互之间有序,那如何保证不同的CommandLineRunner的执行顺序呢?Spring Boot 也给出了解决方案。那就是使用@Order注解。

我们创建两个CommandLineRunner的实现类来进行测试:

第一个实现类:

@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner1 start to initialize ...");
    }
}

第二个实现类:

@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("The OrderRunner2 start to initialize ...");
    }
}

添加完成之后重新启动,观察执行顺序:

The service to start.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

Tomcat started on port(s): 8080 (http) with context path ''
Started CommandLineRunnerApplication in 3.119 seconds (JVM running for 4.554)
The OrderRunner1 start to initialize ...
The OrderRunner2 start to initialize ...
The Runner start to initialize ...
The service has started.

通过控制台的输出我们发现,添加@Order注解的实现类最先执行,并且@Order()里面的值越小启动越早。

在实践中,使用ApplicationRunner也可以达到相同的目的,两着差别不大。看来使用 Spring Boot 解决初始化资源的问题非常简单。

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/09/spring-boot-initialize-resources-when-project-starts/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Boot项目启动时初始化资源
在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。CommandLineRunner就是专门解决项……
<<上一篇
下一篇>>
文章目录
关闭
目 录