• SpringBoot启动跟踪


    程序启动入口

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

    调试跟踪

    执行初始化方法

    /**
         * Create a new {@link SpringApplication} instance. The application context will load
         * beans from the specified sources (see {@link SpringApplication class-level}
         * documentation for details. The instance can be customized before calling
         * {@link #run(String...)}.
         * @param sources the bean sources
         * @see #run(Object, String[])
         * @see #SpringApplication(ResourceLoader, Object...)
         */
        public SpringApplication(Object... sources) {
            initialize(sources);
        }

    初始化方法

     applicationContext和applicationListener方法详情

    /**
         * Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
         * {@link ApplicationContext}.
         * @param initializers the initializers to set
         */
        public void setInitializers(
                Collection<? extends ApplicationContextInitializer<?>> initializers) {
            this.initializers = new ArrayList<ApplicationContextInitializer<?>>();
            this.initializers.addAll(initializers);
        }
    ApplicationContextInitializer初始化applicationContext
    /**
         * Sets the {@link ApplicationListener}s that will be applied to the SpringApplication
         * and registered with the {@link ApplicationContext}.
         * @param listeners the listeners to set
         */
        public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
            this.listeners = new ArrayList<ApplicationListener<?>>();
            this.listeners.addAll(listeners);
        }

    注册listener到apllicationContext

    applicationContext和applicationlistener初始化都是通过getSpringFactoriesInstances实现的,我们一探究竟

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

    找到了关键节点spring.factories文件,我们查看spring boot下的这个文件看看情况

    通过spring.factories文件拿到一系列的Context和Listener之后 执行run方法

    run方法会从spring.factories文件中获取到run listener,然后在spring boot 执行到各个阶段时执行Listener事件和Context事件

     最后的run方法如下

    了解启动流程,方便优化,下边是网友遇到的问题以及优化方案

    https://segmentfault.com/a/1190000004252885

  • 相关阅读:
    宠物店4.0的安装
    《professional asp.net 2.0》读书笔记连载2
    《xhtml 入门系列》之一
    ALinq 让Mysql变得如此简单
    ALinq 入门学习(八)ALinq 对Vs2010 的支持
    教你一款极为简单实用的图表插件
    虚拟机下无法启动 Linux 系统
    怎样去突破文件依赖缓存
    jQuery 表单验证扩展(五)
    Log4Net 全方位跟踪程序运行
  • 原文地址:https://www.cnblogs.com/mongo/p/8340886.html
Copyright © 2020-2023  润新知