ApplicationRunner、InitializingBean、@PostConstruct执行顺序解读

 更新时间:2025年09月13日 11:35:57   作者:_江屿_  
Spring Boot提供CommandLineRunner与ApplicationRunner接口,用于启动时执行初始化任务,前者接收原始参数数组,后者封装为ApplicationArguments对象,两者执行顺序可通过@Order控制

概述

开发中可能会有这样的场景,需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。

SpringBoot给我们提供了两个接口来帮助我们实现这种需求。

两个启动加载接口分别是:CommandLineRunner和ApplicationRunner。

Spring 提供了接口 InitializingBean,jdk提供了@PostConstruct

CommandLineRunner和ApplicationRunner区别

CommandLineRunner和ApplicationRunner的作用是相同的。不同之处在于CommandLineRunner接口的run()方法接收String数组作为参数,即是最原始的参数,没有做任何处理;而ApplicationRunner接口的run()方法接收ApplicationArguments对象作为参数,是对原始参数做了进一步的封装。

当程序启动时,我们传给main()方法的参数可以被实现CommandLineRunner和ApplicationRunner接口的类的run()方法访问,即可接收启动服务时传过来的参数。我们可以创建多个实现CommandLineRunner和ApplicationRunner接口的类。为了使他们按一定顺序执行,可以使用@Order注解或实现Ordered接口。

ApplicationRunner接口的示例

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@Order(value = 1)
public class JDDRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {

        System.out.println("这个是测试ApplicationRunner接口");
               String strArgs = Arrays.stream(arg0.getSourceArgs()).collect(Collectors.joining("|"));
        System.out.println("Application started with arguments:" + strArgs);
    }
}

启动时候指定参数:java -jar xxxx.jar data1 data2 data3

这个是测试ApplicationRunner接口
Application started with arguments:data1|data2|data3

CommandLineRunner接口示例

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("这个是测试CommandLineRunn接口");
         String strArgs = Arrays.stream(args).collect(Collectors.joining("|"));
        System.out.println("Application started with arguments:" + strArgs);
    }
}

启动时候指定参数:java -jar xxxx.jar data1 data2 data3

运行结果:

这个是测试CommandLineRunn接口
Application started with arguments:data1|data2|data3

CommandLineRunner和ApplicationRunner的执行顺序

在spring boot程序中,我们可以使用不止一个实现CommandLineRunner和ApplicationRunner的bean。

为了有序执行这些bean的run()方法,可以使用@Order注解或Ordered接口。

@Component
@Order(2)
public class ApplicationRunnerBean1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 1");
    }
}
@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 2");
    }
}
@Component
@Order(1)
public class CommandLineRunnerBean1 implements CommandLineRunner {
    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 1");
    }
}
@Component
@Order(3)
public class CommandLineRunnerBean2 implements CommandLineRunner {
    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 2");
    }
}

执行结果:

CommandLineRunnerBean 1 ApplicationRunnerBean 1 CommandLineRunnerBean 2 ApplicationRunnerBean 2

实现多个ApplicationRunner时部分接口未执行

@Component
@Slf4j
public class RunnerTest1 implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {

        while (true) {
            System.out.println("this is RunnerTest1");
            Thread.sleep(100);
        }

    }
}
@Component
@Slf4j
public class RunnerTest2 implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {

        while (true) {
            System.out.println("this is RunnerTest2");
            Thread.sleep(100);
        }

    }
}

只会执行RunnerTest1中方法。

通过分析springboot启动的源码可以发现,在applicationContext容器加载完成之后,会调用SpringApplication类的callRunners方法:

该方法中会获取所有实现了ApplicationRunner和CommandLineRunner的接口bean,然后依次执行对应的run方法,并且是在同一个线程中执行。因此如果有某个实现了ApplicationRunner接口的bean的run方法一直循环不返回的话,后续的代码将不会被执行。

InitializingBean接口的用法

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。

注意,实现该接口的最好加上Spring的注解注入,比如@Component

@PostConstruct注解的用法

如果想在生成对象时候完成某些初始化操作,而偏偏这些初始化操作又依赖于依赖注入,那么就无法在构造函数中实现。为此,可以使用@PostConstruct注解一个方法来完成初始化,@PostConstruct注解的方法将会在依赖注入完成后被自动调用。

优先级: Constructor >> @Autowired >> @PostConstruct

@Component
public class Test implements InitializingBean, ApplicationRunner, CommandLineRunner {

    @PostConstruct
    public void init(){
        System.out.println("PostConstruct 方法执行");
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean 方法执行");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("这个是测试ApplicationRunner接口");

    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("这个是测试CommandLineRunn接口");
    }
}
PostConstruct 方法执行 InitializingBean 方法执行
这个是测试ApplicationRunner接口 这个是测试CommandLineRunn接口

由此可知: @PostConstruct>InitializingBean>ApplicationRunner>CommandLineRunner

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用springboot防止反编译proguard+xjar

    使用springboot防止反编译proguard+xjar

    介绍了三种代码混淆和加密工具的使用方法:ProGuard、Xjar和ClassFinal,ProGuard用于混淆Java字节码,Xjar提供对JAR包内资源的加密和动态解密,而ClassFinal则支持直接加密JAR包或WAR包,通过预研和实际操作
    2024-11-11
  • maven多模块pom配置实例详解

    maven多模块pom配置实例详解

    这篇文章主要介绍了maven多模块pom配置实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • java远程连接Linux执行命令的3种方式完整代码

    java远程连接Linux执行命令的3种方式完整代码

    在一些Java应用程序中需要执行一些Linux系统命令,例如服务器资源查看、文件操作等,这篇文章主要给大家介绍了关于java远程连接Linux执行命令的3种方式,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-06-06
  • java文件读写工具类分享

    java文件读写工具类分享

    这篇文章主要为大家详细介绍了java文件读写工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • dubbo filter中有关bean注入和配置文件读取方式

    dubbo filter中有关bean注入和配置文件读取方式

    这篇文章主要介绍了dubbo filter中有关bean注入和配置文件读取方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • Java创建内部类对象实例详解

    Java创建内部类对象实例详解

    这篇文章主要介绍了Java创建内部类对象实例详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • SpringBoot3+SpringSecurity6前后端分离的项目实践

    SpringBoot3+SpringSecurity6前后端分离的项目实践

    SpringSecurity6 的用法和以前版本的有较大差别,本文主要介绍了SpringBoot3+SpringSecurity6前后端分离的项目实践,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • SpringBoot中的@ConfigurationProperties注解解析

    SpringBoot中的@ConfigurationProperties注解解析

    这篇文章主要介绍了SpringBoot中的@ConfigurationProperties注解解析,Spring源码中大量使用了ConfigurationProperties注解,通过与其他注解配合使用,能够实现Bean的按需配置,该注解可以放在类上,也可以放在方法上,需要的朋友可以参考下
    2023-11-11
  • 一文看懂RabbitMQ消息丢失如何防止

    一文看懂RabbitMQ消息丢失如何防止

    这篇文章主要介绍了RabbitMQ消息丢失的场景,以及如何保证信息不丢失,看完这篇文章一定可以帮助你RabbitMQ有更深的理解,需要的朋友可以参考下
    2023-03-03
  • 使用Java实现查找并移除字符串中的Emoji

    使用Java实现查找并移除字符串中的Emoji

    Emoji 实际上是 UTF-8 (Unicode) 字符集上的特殊字符,这篇文章主要介绍了如何使用Java实现查找并移除字符串中的Emoji,感兴趣的可以了解下
    2024-03-03

最新评论