SpringBoot启动的核心方法 run

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        FailureAnalyzers analyzers = null;
        configureHeadlessProperty();
 
        // 从spring.factories中加载SpringApplicationRunListener
        SpringApplicationRunListeners listeners = getRunListeners(args);
 
        // 该方法内部会调用EventPublishingRunListener.starting()方法
        // 广播ApplicationStartedEvent事件
        // LoggingApplicationListener监听该事件,进行日志初始化
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
             
            // 创建ConfigurableEnvironment对象
            // 配置Environment
            //      1)配置propertySource,添加commandLinePropertySource和defaultProperty
            //      2)配置active profile
            // 广播ApplicationEnvironmentPreparedEvent事件
            // ConfigFileApplicationListener监听该事件,读取appliation.properties配置
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
 
            // 打印banner, 可通过banner.txt覆盖默认banner
            Banner printedBanner = printBanner(environment);
 
            // 创建ApplicationContext,继承自GenericApplicationContext
            // GenericApplicationContext实例化DefaultListableBeanFactory
            context = createApplicationContext();
 
            // 从spring.factories中加载错误分析器,用于分析上报启动错误
            analyzers = new FailureAnalyzers(context);
 
            // 从spring.factories中加载ApplicationContextInitializer,并执行initialize方法
            // 创建BeanDefinitionLoader实例, 包含以下几个核心类:
            //      1)AnnotatedBeanDefinitionReader, 注册有注解的class
            //          构造函数中又注册以下5个bean(此时BeanDefinitionRegistry共注册5个beanDefinition):
            //              ConfigurationClassPostProcessor
            //              AutowiredAnnotationBeanPostProcessor
            //              CommonAnnotationBeanPostProcessor
            //              EventListenerMethodProcessor
            //              DefaultEventListenerFactory
            //      2)XmlBeanDefinitionReader, 加载xml配置
            //      3)ClassPathBeanDefinitionScanner, 通过componentScan方式扫描bean
            // 调用BeanDefinitionLoader.load()方法注册启动类(入口类XxxApplication),(此时BeanDefinitionRegistry共注册6个beanDefinition)
            // 广播ApplicationPreparedEvent事件
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
 
            // 调用AbstractApplicationContext.refresh(),spring启动核心方法
            refreshContext(context);
 
            // 执行ApplicationRunner和CommandLineRunner
            afterRefresh(context, applicationArguments);
 
            // 广播ApplicationReadyEvent事件或者ApplicationFailedEvent
            listeners.finished(context, null);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            return context;
        }
        catch (Throwable ex) {
            handleRunFailure(context, listeners, analyzers, ex);
            throw new IllegalStateException(ex);
        }
    }

 

你可能感兴趣的:(spring,java)