源码分析SpringBoot启动

遇到一个问题,需要从yml文件中读取数据初始化到static的类中。搜索需要实现ApplicationRunner,并在其实现类中把值读出来再set进去。于是乎就想探究一下SpringBoot启动中都干了什么。

引子

就像引用中说的,用到了ApplicationRunner类给静态class赋yml中的值。代码先量一下,是这样:

@Data
@Component
@EnableConfigurationProperties(MyApplicationRunner.class)
@ConfigurationProperties(prefix = "flow")
public class MyApplicationRunner implements ApplicationRunner {

    private String name;
    private int age;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner...start...");
        MyProperties.setAge(age);
        MyProperties.setName(name);
        System.out.println("ApplicationRunner...end...");
    }
}
public class  MyProperties {
    private static String name;
    private static int age;
    public static String getName() {
        return name;
    }
    public static void setName(String name) {
        MyProperties.name = name;
    }
    public static int getAge() {
        return age;
    }
    public static void setAge(int age) {
        MyProperties.age = age;
    }
}

从SpringApplication开始

@SpringBootApplication
public class FlowApplication {
    public static void main(String[] args) {
        SpringApplication.run(FlowApplication.class, args);
    }
}

这是一个SpringBoot启动入口,整个项目环境搭建和启动都是从这里开始的。我们就从SpringApplication.run()点进去看一下,Spring Boot启动的时候都做了什么。点进去run看一下。

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

首先经过了两个方法,马上就要进入关键了。SpringApplication(primarySources).run(args),这句话做了两件事,首先初始化SpringApplication,然后进行开启run。首先看一下初始化做了什么。

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    setInitializers((Collection) getSpringFactoriesInstances(
            ApplicationContextInitializer.class));
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = deduceMainApplicationClass();
}

首先读取资源文件Resource,然后读取FlowApplication这个类信息(就是primarySources),然后从classPath中确定是什么类型的项目,看一眼WebApplicationType这里面有三种类型:

public enum WebApplicationType {
    NONE, //不是web项目
    SERVLET,//是web项目
    REACTIVE;//2.0之后新加的,响应式项目
    ...
}

回到SpringApplication接着看,确定好项目类型之后,初始化一些信息setInitializers(),getSpringFactoriesInstances()看一下都进行了什么初始化:

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
        Class<?>[] parameterTypes, Object... args) {
    ClassLoader classLoader = getClassLoader();
    // Use names and ensure unique to protect against duplicates
    Set<String> names = new LinkedHashSet<>(
            SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
            classLoader, args, names);
    AnnotationAwareOrderComparator.sort(instances);
    return instances;
}

首先得到ClassLoader,这个里面记录了所有项目package的信息、所有calss的信息啊什么什么的,然后初始化各种instances,在排个序,ruturn之。

再回到SpringApplication,接着是设置监听器setListeners()。

然后设置main方法,mainApplicationClass(),点进deduceMainApplicationClass()看一看:

private Class<?> deduceMainApplicationClass() {
    try {
        StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            if ("main".equals(stackTraceElement.getMethodName())) {
                return Class.forName(stackTraceElement.getClassName());
            }
        }
    }
    catch (ClassNotFoundException ex) {
        // Swallow and continue
    }
    return null;
}

从方法栈stackTrace中,不断读取方法,通过名称,当读到“main”方法的时候,获得这个类实例,return出去。

到这里,所有初始化工作结束了,也找到了Main方法,ruturn给run()方法,进行后续项目的项目启动。

准备好,开始run吧

先上代码:

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                args);
        ConfigurableEnvironment environment = prepareEnvironment(listeners,
                applicationArguments);
        configureIgnoreBeanInfo(environment);
        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.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass)
                    .logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

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

首先开启一个计时器,记录下这次启动时间,咱们项目开启 XXXms statred 就是这么计算来的。
然后是一堆声明,知道listeners.starting(),这个starting(),我看了一下源码注释

Called immediately when the run method has first started. Can be used for very
early initialization.

早早初始化,是为了后面使用,看到后面还有一个方法listeners.started()

Called immediately before the run method finishes, when the application context has been refreshed and all {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner ApplicationRunners} have been called.

这会儿应该才是真正的开启完毕,值得一提的是,这里终于看到了引子中的ApplicationRunner这个类了,莫名的有点小激动呢。

我们继续进入try,接下来是读取一些参数applicationArguments,然后进行listener和environment的一些绑定。然后打印出Banner图,printBanner(),这个方法里面可以看到把environment,也存入Banner里面了,应该是为了方便打印,如果有日志模式,也打印到日志里面,所以,项目启动的打印日志里面记录了很多东西。

private Banner printBanner(ConfigurableEnvironment environment) {
    if (this.bannerMode == Banner.Mode.OFF) {
        return null;
    }
    ResourceLoader resourceLoader = (this.resourceLoader != null)
            ? this.resourceLoader : new DefaultResourceLoader(getClassLoader());
    SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
            resourceLoader, this.banner);
    if (this.bannerMode == Mode.LOG) {
        return bannerPrinter.print(environment, this.mainApplicationClass, logger);
    }
    return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}

接着 生成上下文环境 context = createApplicationContext();还记着webApplicationType三种类型吗,这边是根据webApplicationType类型生成不同的上下文环境类的。

接着开启 exceptionReporters,用来支持启动时的报错。

接着就要准备往上下文中set各种东西了,看prepareContext()方法:

private void prepareContext(ConfigurableApplicationContext context,
        ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
        ApplicationArguments applicationArguments, Banner printedBanner) {
    context.setEnvironment(environment);
    postProcessApplicationContext(context);
    applyInitializers(context);
    listeners.contextPrepared(context);
    if (this.logStartupInfo) {
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }
    // Add boot specific singleton beans
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    if (printedBanner != null) {
        beanFactory.registerSingleton("springBootBanner", printedBanner);
    }
    if (beanFactory instanceof DefaultListableBeanFactory) {
        ((DefaultListableBeanFactory) beanFactory)
                .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    }
    // Load the sources
    Set<Object> sources = getAllSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[0]));
    listeners.contextLoaded(context);
}

首先把环境environment放进去,然后把resource信息也放进去,再让所有的listeners知道上下文环境。 这个时候,上下文已经读取yml文件了 所以这会儿引子中yml创建的参数,上下文读到了配置信息,又有点小激动了!接着看,向beanFactory注册单例bean:一个参数bean,一个Bannerbean。

prepareContext() 这个方法大概先这样,然后回到run方法中,看看项目启动还干了什么。

refreshContext(context) 接着要刷新上下问环境了,这个比较重要,也比较复杂,今天只看个大概,有机会另外写一篇博客,说说里面的东西,这里面主要是有个refresh()方法。看注释可知,这里面进行了Bean工厂的创建,激活各种BeanFactory处理器,注册BeanPostProcessor,初始化上下文环境,国际化处理,初始化上下文事件广播器,将所有bean的监听器注册到广播器(这样就可以做到Spring解耦后Bean的通讯了吧)

总之,Bean的初始化我们已经做好了,他们直接也可以很好的通讯。

接着回到run方法,
afterRefresh(context, applicationArguments); 这方法里面没有任何东西,网上查了一下,说这里是个拓展点,有机会研究下。

接着stopWatch.stop();启动就算完成了,因为这边启动时间结束了。
我正要失落的发现没找到我们引子中说到的ApplicationRunner这个类,就在下面看到了最后一个方法,必须贴出来源码:
callRunners(context, applicationArguments),当然这个方法前面还有listeners.started().

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);
    for (Object runner : new LinkedHashSet<>(runners)) {
        if (runner instanceof ApplicationRunner) {
            callRunner((ApplicationRunner) runner, args);
        }
        if (runner instanceof CommandLineRunner) {
            callRunner((CommandLineRunner) runner, args);
        }
    }
}

看到没,这会就执行了ApplicationRunner 方法(至于CommandLineRunner,和ApplicationRunner类似,只是参数类型不同,这边不做过多区分先)。所以可以说ApplicationRunner不是启动的一部分,不记录进入SpringBoot启动时间内,这也好理解啊,你自己初始化数据的时间凭什么算到我SpringBoot身上,你要初始化的时候做了个费时操作,回头又说我SpringBoot辣鸡,那我不是亏得慌...

最后run下这个,listeners.running(context);这会儿用户自定义的事情也会被调用了。

ok,结束了。

小结

今天只是大概看了下SpringBoot启动过程。有很多细节,比如refresh()都值得再仔细研究一下。SpringBoot之所以好用,就是帮助我们做了很多配置,省去很多细节(不得不说各种stater真实让我们傻瓜式使用了很多东西),但是同样定位bug或者通过项目声明周期搞点事情的时候会无从下手。所以,看看SpringBoot源码还是听有必要的。

原文地址:https://www.cnblogs.com/pjjlt/p/10990595.html

时间: 2024-08-12 10:44:50

源码分析SpringBoot启动的相关文章

Linux内核源码分析--内核启动之(5)Image内核启动(rest_init函数)(Linux-3.0 ARMv7)【转】

原文地址:Linux内核源码分析--内核启动之(5)Image内核启动(rest_init函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.chinaunix.net/uid-25909619-id-4938395.html 前面粗略分析start_kernel函数,此函数中基本上是对内存管理和各子系统的数据结构初始化.在内核初始化函数start_kernel执行到最后,就是调用rest_init函数,这个函数的主要使命就是创建并启动内核线

Linux内核源码分析--内核启动之(6)Image内核启动(do_basic_setup函数)(Linux-3.0 ARMv7)【转】

原文地址:Linux内核源码分析--内核启动之(6)Image内核启动(do_basic_setup函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.chinaunix.net/uid-25909619-id-4938396.html 在基本分析完内核启动流程的之后,还有一个比较重要的初始化函数没有分析,那就是do_basic_setup.在内核init线程中调用了do_basic_setup,这个函数也做了很多内核和驱动的初始化工作,详解

Linux内核源码分析--内核启动之(3)Image内核启动(C语言部分)(Linux-3.0 ARMv7) 【转】

原文地址:Linux内核源码分析--内核启动之(3)Image内核启动(C语言部分)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.chinaunix.net/uid-25909619-id-4938390.html 在构架相关的汇编代码运行完之后,程序跳入了构架无关的内核C语言代码:init/main.c中的start_kernel函数,在这个函数中Linux内核开始真正进入初始化阶段, 下面我就顺这代码逐个函数的解释,但是这里并不会过于深入

Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7)【转】

原文地址:Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.chinaunix.net/uid-25909619-id-4938393.html 在分析start_kernel函数的时候,其中有构架相关的初始化函数setup_arch. 此函数根据构架而异,对于ARM构架的详细分析如下: void __init setup_arch(char **cmdlin

Nginx源码分析 - Nginx启动以及IOCP模型

Nginx 源码分析 - Nginx启动以及IOCP模型 版本及平台信息 本文档针对Nginx1.11.7版本,分析Windows下的相关代码,虽然服务器可能用linux更多,但是windows平台下的代码也基本相似 ,另外windows的IOCP完成端口,异步IO模型非常优秀,很值得一看. Nginx启动 曾经有朋友问我,面对一个大项目的源代码,应该从何读起呢?我给他举了一个例子,我们学校大一大二是在紫金港校区,到了 大三搬到玉泉校区,但是大一的时候也会有时候有事情要去玉泉办.偶尔会去玉泉,但

[SpringBoot]源码分析SpringBoot的异常处理机制

微信号:GitShare微信公众号:爱折腾的稻草如有问题或建议,请在公众号留言[1] 前续 为帮助广大SpringBoot用户达到"知其然,更需知其所以然"的境界,作者将通过SpringBoot系列文章全方位对SpringBoot2.0.0.RELEASE版本深入分解剖析,让您深刻的理解其内部工作原理. 正文 在SpringBoot启动时,会查找并加载所有可用的SpringBootExceptionReporter,其源码如下: //7 使用SpringFactoriesLoader在

memcached源码分析-----memcached启动参数详解以及关键配置的默认值

转载请注明出处: http://blog.csdn.net/luotuo44/article/details/42672913 本文开启本系列博文的代码分析.本系列博文研究是memcached版本是1.4.21. 本文将给出memcached启动时各个参数的详细解释以及一些关键配置的默认值.以便在分析memcached源码的时候好随时查看.当然也方便使用memcached时可以随时查看各个参数的含义.<如何阅读memcached源码>说到memcached有很多全局变量(也就是关键配置),这些

安卓MonkeyRunner源码分析之启动

在工作中因为要追求完成目标的效率,所以更多是强调实战,注重招式,关注怎么去用各种框架来实现目的.但是如果一味只是注重招式,缺少对原理这个内功的了解,相信自己很难对各种框架有更深入的理解. 从几个月前开始接触ios和android的自动化测试,原来是本着仅仅为了提高测试团队工作效率的心态先行作浅尝即止式的研究,然后交给测试团队去边实现边自己研究,最后因为各种原因果然是浅尝然后就止步了,而自己最终也离开了上一家公司.换了工作这段时间抛开所有杂念和以前的困扰专心去学习研究各个框架的使用,逐渐发现这还是

MonkeyRunner源码分析之启动

在工作中因为要追求完成目标的效率,所以更多是强调实战,注重招式,关注怎么去用各种框架来实现目的.但是如果一味只是注重招式,缺少对原理这个内功的了解,相信自己很难对各种框架有更深入的理解. 从几个月前开始接触ios和android的自动化测试,原来是本着仅仅为了提高测试团队工作效率的心态先行作浅尝即止式的研究,然后交给测试团队去边实现边自己研究,最后因为各种原因果然是浅尝然后就止步了,而自己最终也离开了上一家公司.换了工作这段时间抛开所有杂念和以前的困扰专心去学习研究各个框架的使用,逐渐发现这还是