美文网首页
02--SpringBoot启动源码简单分析

02--SpringBoot启动源码简单分析

作者: 闲来也无事 | 来源:发表于2018-08-19 00:10 被阅读74次

打开SampleSimpleApplication.java,我们可以看到

public static void main(String[] args) {
    //SpringBoot启动函数
    SpringApplication.run(SampleSimpleApplication.class, args);
}

跟踪进去,可看到

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class<?>[]{primarySource}, args);
}

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return new SpringApplication(primarySources).run(args);
}

其中return new SpringApplication(primarySources).run(args);为核心方法,创建SpringApplication实例并执行run()方法,接下来我们对这句话进行分析.

1. 创建SpringApplication实例

public SpringApplication(Class<?>... primarySources) {
    this(null, primarySources);
}

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    //resourceLoader赋值给this.resourceLoader
    this.resourceLoader = resourceLoader;
    //断言
    Assert.notNull(primarySources, "PrimarySources must not be null");
    //将primarySources存入LinkedHashSet对象并赋值给this.primarySources
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    //推断当前应用类型
    this.webApplicationType = deduceWebApplicationType();
    //初始化Initializer
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    //初始化Listener
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    //推断主类
    this.mainApplicationClass = deduceMainApplicationClass();
    }

2. 执行run()完成SpringBoot启动

public ConfigurableApplicationContext run(String... args) {
    //创建StopWatch对象,用来记录程序启动耗时
    StopWatch stopWatch = new StopWatch();
    //启动
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    //根据args获取所有SpringApplicationRunListeners监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    //启动监听器,首次启动run方法时立即调用
    listeners.starting();
    try {
        //创建ApplicationArguments对象,并将args封装至对象实例
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        //准备环境
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        //配置需要忽略的bean
        configureIgnoreBeanInfo(environment);
        //打印banner
        Banner printedBanner = printBanner(environment);
        //创建应用上下文对象
        context = createApplicationContext();
        //获取异常报告对象
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
        //准备上下文环境
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        //刷新上下文
        refreshContext(context);
        //刷新上下文后续处理
        afterRefresh(context, applicationArguments);
        //关闭stopWatch
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        //执行监听器started方法
        listeners.started(context);
        callRunners(context, applicationArguments);
        } catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, listeners);
            throw new IllegalStateException(ex);
        }
        try {
            //在run方法完成之前立即调用,刷新应用程序上下文并且所有的CommandLineRunners和ApplicationRunners已经被调用
            listeners.running(context);
        } catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }

至此,我们已经可以大概了解到SpringBoot的启动流程,因为启动过程中的一些方法比较复杂,所以我们接下来会分篇幅介绍其中一些核心的方法...

相关文章

网友评论

      本文标题:02--SpringBoot启动源码简单分析

      本文链接:https://www.haomeiwen.com/subject/gtvqiftx.html