@PostConstruct、InitializingBean、 ApplicationRunner 和 CommandLineRunner的执行顺序

在实际开发场景中,经常会遇到数据初始化的业务场景,那么如何在Spring容器启动时执行相关操作呢?比如:读取配置文件,数据库连接,数据缓存,数据预计算…
可以使用:@PostConstruct、InitializingBean、 ApplicationRunner 和 CommandLineRunner来完成。

测试示例

package com.study;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class TempApplication implements InitializingBean, ApplicationRunner, CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(TempApplication.class);

    public static void main(String[] args) {
        logger.info("----------开始----------");
        SpringApplication.run(TempApplication.class, args);
        logger.info("----------结束----------");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("----------InitializingBean----------");
    }

    @PostConstruct
    public void init() {
        logger.info("----------@PostConstruct----------");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("----------ApplicationRunner----------");
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("----------CommandLineRunner----------");
    }

    @Component
    class MyBean implements InitializingBean, ApplicationRunner, CommandLineRunner {

        @Override
        public void afterPropertiesSet() throws Exception {
            logger.info("----------InitializingBean2----------");
        }

        @PostConstruct
        public void init() {
            logger.info("----------@PostConstruct2----------");
        }

        @Override
        public void run(ApplicationArguments args) throws Exception {
            logger.info("----------ApplicationRunner2----------");
        }

        @Override
        public void run(String... args) throws Exception {
            logger.info("----------CommandLineRunner2----------");
        }
    }
}

执行结果:

----------开始----------
----------@PostConstruct----------
----------InitializingBean----------
----------@PostConstruct2----------
----------InitializingBean2----------
----------ApplicationRunner----------
----------CommandLineRunner----------
----------ApplicationRunner2----------
----------CommandLineRunner2----------
----------结束----------

发现放在不同的位置,执行结果有2种情况:

情况1:@PostConstruct > InitializingBean > ApplicationRunner > CommandLineRunner

情况2:InitializingBean > @PostConstruct2 >ApplicationRunner2 > CommandLineRunner2

 

区别描述

1. 如果需要在容器启动后进行一些初始化操作,可以使用由SpringBoot提供的ApplicationRunner/CommandLineRunner;
2. 如果需要在一个类加载的时候进行一个初始化的操作,可以选择由JDK提供的@PostConstruct注解进行一个初始化的操作。另:@PostConstruct可以在容器没有完全启动的情况下能够进行初始化操作,而ApplicationRunner/CommandLineRunner的初始化一定是在容器完全启动之后执行的。@PostConstruct这个注解是针对BEAN的初始化完成之后再做一些事情,比如注册一些监听器、配置文件的初始化等等;
3. 由Spring提供的InitializingBean用法与@PostConstruct基本一致,但是相应的BEAN需要实现afterPropertiesSet()方法;

定义来源

名称来源
ApplicationRunnerSpring Boot
CommandLineRunnerSpring Boot
InitializingBeanSpring
@PostConstructJDK

参考:

SpringBoot - @PostConstruct、InitializingBean、 ApplicationRunner 和 CommandLineRunner的执行顺序_cloneme01的博客-CSDN博客

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值