Spring Boot启动时如何调用特定方法的解析过程?

2026-06-10 09:5912阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计553个文字,预计阅读时间需要3分钟。

Spring Boot启动时如何调用特定方法的解析过程?

Spring Boot提供了两种启动自动化的方式:启动自动启动和命令行启动。这两种方式分别是通过ApplicationRunner和CommandLineRunner实现的,目的是为了在容器启动时执行一些初始化操作。我们可以通过实现ApplicationRunner或CommandLineRunner来执行所需的方法。

Spring Boot提供了两种 “开机自启动” 的方式,ApplicationRunner和CommandLineRunner

这两种方式的目的是为了满足,在容器启动时like执行某些方法。我们可以通过实现ApplicationRunner或者CommandLineRunner来实现,他们都是在SpringAppliaction执行之后开始执行的。这个特性可以让我们自定义一些在容器启动时需要初始化的逻辑

Spring Boot启动时如何调用特定方法的解析过程?

ApplicationRunner接口:

官方doc:

Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple ApplicationRunner beans can be defined within the same application context and can be ordered using the Ordered

当该接口包含在SpringApplication中时执行。多个ApplicationRunner通过Order直接进行排序:

/** * 初始化类 */ @Order(1) // @Order注解可以改变执行顺序,越小越先执行 @Component public class MyApplicationRunner1 implements ApplicationRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("MyApplicationRunner1----" + arg0); } }

/** * 初始化类 */ @Order(2) @Component public class MyApplicationRunner2 implements ApplicationRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("MyApplicationRunner2----" + arg0); } }

容器启动后的运行结果:

可以看到多个ApplicationRunner执行顺序是按照Order中的值执行的,并且每个的入参都是同一个ApplicationArguments实例(具体原因后面分析)

CommandLineRunner接口:

二者的官方doc基本一样,区别在于接收的参数不一样

/** * 初始化类 */ @Order(1) // @Order注解可以改变执行顺序,越小越先执行 @Component public class MyCommandLineRunner1 implements CommandLineRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(String... args) throws Exception { System.out.println("MyCommandLineRunner1----" + args); } }

/** * 初始化类 */ @Order(2) @Component public class MyCommandLineRunner2 implements CommandLineRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(String... args) throws Exception { System.out.println("MyCommandLineRunner2----" + args); } }

容器启动后的运行结果:

可以看到多个CommandLineRunner的执行效果跟ApplicationRunner一模一样

最后看下源码:

SpringApplication启动时,会执行其run方法中的afterRefresh方法:

在afterRefresh中可以看到这两个接口被执行,并且每个ApplicationRunner或CommandLineRunner实例都是用的同一个入参:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计553个文字,预计阅读时间需要3分钟。

Spring Boot启动时如何调用特定方法的解析过程?

Spring Boot提供了两种启动自动化的方式:启动自动启动和命令行启动。这两种方式分别是通过ApplicationRunner和CommandLineRunner实现的,目的是为了在容器启动时执行一些初始化操作。我们可以通过实现ApplicationRunner或CommandLineRunner来执行所需的方法。

Spring Boot提供了两种 “开机自启动” 的方式,ApplicationRunner和CommandLineRunner

这两种方式的目的是为了满足,在容器启动时like执行某些方法。我们可以通过实现ApplicationRunner或者CommandLineRunner来实现,他们都是在SpringAppliaction执行之后开始执行的。这个特性可以让我们自定义一些在容器启动时需要初始化的逻辑

Spring Boot启动时如何调用特定方法的解析过程?

ApplicationRunner接口:

官方doc:

Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple ApplicationRunner beans can be defined within the same application context and can be ordered using the Ordered

当该接口包含在SpringApplication中时执行。多个ApplicationRunner通过Order直接进行排序:

/** * 初始化类 */ @Order(1) // @Order注解可以改变执行顺序,越小越先执行 @Component public class MyApplicationRunner1 implements ApplicationRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("MyApplicationRunner1----" + arg0); } }

/** * 初始化类 */ @Order(2) @Component public class MyApplicationRunner2 implements ApplicationRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("MyApplicationRunner2----" + arg0); } }

容器启动后的运行结果:

可以看到多个ApplicationRunner执行顺序是按照Order中的值执行的,并且每个的入参都是同一个ApplicationArguments实例(具体原因后面分析)

CommandLineRunner接口:

二者的官方doc基本一样,区别在于接收的参数不一样

/** * 初始化类 */ @Order(1) // @Order注解可以改变执行顺序,越小越先执行 @Component public class MyCommandLineRunner1 implements CommandLineRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(String... args) throws Exception { System.out.println("MyCommandLineRunner1----" + args); } }

/** * 初始化类 */ @Order(2) @Component public class MyCommandLineRunner2 implements CommandLineRunner { /** * 会在服务启动完成后立即执行 */ @Override public void run(String... args) throws Exception { System.out.println("MyCommandLineRunner2----" + args); } }

容器启动后的运行结果:

可以看到多个CommandLineRunner的执行效果跟ApplicationRunner一模一样

最后看下源码:

SpringApplication启动时,会执行其run方法中的afterRefresh方法:

在afterRefresh中可以看到这两个接口被执行,并且每个ApplicationRunner或CommandLineRunner实例都是用的同一个入参:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。