Spring Boot之 CommandLineRunner、ApplicationRunner和@PostConstruct

在使用Spring Boot开发的工作中,我们经常会需要遇到一种功能需求,比如在服务启动时候,去加载一些配置,去请求一下其他服务的接口。Spring Boot给我们提供了三种常用的实现方法:
第一种是实现CommandLineRunner接口,
第二种是实现ApplicationRunner接口
第三种是使用注解:@PostConstruct

1、CommandLineRunner

1、CommandLineRunner执行的时间节点是在Application完成初始化工作之后。
2、CommandLineRunner在有多个实现的时候,可以使用@order注解指定执行先后顺序。
3、源码在:org.springframework.boot.SpringApplication#run(),可以看看

我们先看一下CommandLineRunner的源码:

package org.springframework.boot;

@FunctionalInterface
public interface CommandLineRunner {
    void run(String... args) throws Exception;
}

SpringApplication源码:

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
        ConfigurableApplicationContext context = null;
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting(bootstrapContext, this.mainApplicationClass);

        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            context.setApplicationStartup(this.applicationStartup);
            this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

callRunners方法源码:

private void callRunners(ApplicationContext context, ApplicationArguments args) {
        List<Object> runners = new ArrayList();
        runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
        runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
        AnnotationAwareOrderComparator.sort(runners);
        Iterator var4 = (new LinkedHashSet(runners)).iterator();

        while(var4.hasNext()) {
            Object runner = var4.next();
            if (runner instanceof ApplicationRunner) {
                this.callRunner((ApplicationRunner)runner, args);
            }

            if (runner instanceof CommandLineRunner) {
                this.callRunner((CommandLineRunner)runner, args);
            }
        }

    }

我们写一个例子实现:

 
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Order(1)
public class CommandLineRunnerTest implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("----CommandLineRunnerTest1 start---"+ Arrays.toString(args));
    }
}

2、ApplicationRunner

ApplicationRunner跟CommandLineRunner是区别是在run方法里接收的参数不同,CommandLineRuner接收的参数是String... args,而ApplicationRunner的run方法的参数是ApplicationArguments 

看看ApplicationRunner的源码:

package org.springframework.boot;

@FunctionalInterface
public interface ApplicationRunner {
    void run(ApplicationArguments args) throws Exception;
}

我们写一个例子实现:


import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
@Order(1)
public class ApplicationRunnerTest implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("---ApplicationRunnerTest start----");

        List<String> nonOptionArgs = args.getNonOptionArgs();
        System.out.println("[非选项参数]>>> " + nonOptionArgs);
        Set<String> optionNames = args.getOptionNames();
        for(String optionName: optionNames) {
            System.out.println("[选项参数]>>> name:" + optionName
                    + ";value:" + args.getOptionValues(optionName));
        }
    }
}

3、@PostConstruct

@PostConstruct是在javaEE5的时候引入的,它并不是Spring提供的,但是Spring有对@PostConstruct的实现。并且是在对象加载完之后执行。

先看注解源码

@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}

我们写一个例子实现:

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class PostConstructTest {

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


}

运行代码输出结果 :

5、源码

https://gitee.com/Qinux/command-line-runner-demo.git

微信公众号:一凡码农
欢迎交流
Spring Boot 中,`CommandLineRunner` 是一种方便的方式来在应用程序启动时运行一些初始化任务,比如数据迁移、日志配置等。如果你想在 `run` 方法执行完后注入依赖的 Bean,通常不会直接在 `run` 方法内部注入,因为这会违背单例模式,可能导致并发问题。 然而,你可以通过以下几种方式间接实现: 1. **使用 @PostConstruct 注解**: 如果你想确保某个Bean在 `CommandLineRunner` 完成后立即执行,可以在该Bean上添加 `@PostConstruct` 注解,Spring会在所有 `CommandLineRunner` 执行完毕后再调用这个方法。 ```java @Component public class MyComponent { @Autowired private SomeDependency dependency; // 使用 @PostConstruct 注解的方法 @PostConstruct public void init() { // 这里可以使用已经注入的 dependency } // CommandLineRunner 的 run 方法 @Override public void run(String... args) throws Exception { // ... } } ``` 2. **使用 ApplicationRunner** 或者 **ApplicationArgumentsRunner** 替代 CommandLineRunner: 这些接口允许你在应用启动过程中执行更细粒度的任务,并且它们允许在每个命令行参数处理后或整个参数处理完成后注册回调方法。 ```java @Component public class MyApplicationRunner implements ApplicationRunner { @Autowired private SomeDependency dependency; @Override public void run(ApplicationArguments args) { // 在这里,args.getApplicationArguments() 可以获取参数 // 然后在合适的地方注入 dependency // ... } } ``` 3. **Spring Boot Actuator**: 如果是用于服务启动后的配置,可以考虑使用 Actuator 提供的工具如 HealthIndicator 或 MetricsEndpoint,它们会在启动后提供注册点。 重要的是,确保你的 Bean 配置为 Spring 容器管理,并且在整个生命周期内可用,而不是仅在特定的执行阶段。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值