SpringBoot entry (seven) ------ run custom startup class method

springboot project will have a startup class, this is the entrance of our project, but when we want the project to start to do some other things, can not use the original startup class, and we look at the source of the run method:
    public ConfigurableApplicationContext run(String... args) {
		// 创建计时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
		
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
		// 设置属性
        this.configureHeadlessProperty();
		
		// spring中监听器的使用,这些对象想创建,就得知道监听器的全路径
		// 会从spring.factory中读取
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
			// 初始化默认应用参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			// 根据监听器和默认的参数 来准备spring所需要的环境
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
			// 打印出banner
            Banner printedBanner = this.printBanner(environment);
			// 创建应用上下文
            context = this.createApplicationContext();
			// 异常报告
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
			// 准备应用上下文	
            this.prepareContext(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);
			// 执行callRunners, 支持自定义run方法
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

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

We found this method try block of the last rowcallRunnersThe method is to support the run custom method, and then point to go see

    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);
            }
        }

    }

See ApplicationRunner, CommandLineRunner these two classes, two classes that support custom run method of
procedure is as follows:
Start class implements ApplicationRunner or CommandLineRunner interface logic and the like need to perform the code written into the re-run method

package com.jym.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author jym
 */
@SpringBootApplication()
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplication(DemoApplication.class).run();
    }

    public void run(String... args) throws Exception {
        System.out.println("jymQWQ");
    }
}

So that when the project started, the code we need to do on the implementation of the

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Thank you for watching, if not where to write your message, thank you.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104079438