【源码分析】Springboot启动流程源码分析-创新互联

1.简介

springboot版本:2.7.2

城关网站建设公司创新互联,城关网站设计制作,有大型网站制作公司丰富经验。已为城关1000多家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的城关做网站的公司定做!

SpringApplication类是用来执行Spring框架启动的引导类。
有两种方式可以进行启动引导:

  • 通过静态方法SpringApplication.run启动。
  • 先创建SpringApplication实例,在调用的实例方法run进行启动。
    无论是以上哪种方式,最终都是通过创建SpringApplication实例,在调用run()启动。
public static void main(String[] args) {SpringApplication.run(StateMachineApplication.class, args);
    }
2.源码总结分析

类:org.springframework.boot.SpringApplication

run(String… args) 方法分析

主要方法:下面

public ConfigurableApplicationContext run(String... args) {long startTime = System.nanoTime();
        //初始化启动上下文
        DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
        ConfigurableApplicationContext context = null;
        //配置Headless模式:Headless模式是在环境缺少显示器等设备情况下的一种配置,和启动流程并无太多关系
        this.configureHeadlessProperty();
        //listeners 监听器可以用来监听SpringApplication启动过程中的各个阶段。
        //默认的监听器是EventPublishRunListener,用户也可以通过实现SpringApplicationRunListener接口,实现应用程序对SpringApplication启动过程的监听。
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting(bootstrapContext, this.mainApplicationClass);
        try {//准备环境,创建ConfigurableEnvironment对象
        //SpringApplication会创建Spring启动所需的环境,这个环境主要由ConfigurableEnviroment对象表示。首先,该对象确认了程序是否需要设置Web环境,其次,该对象还确定了程序所需要的参数和读取的配置文件等信息。此步骤会回调SpringApplicationRunListener的environmentPrepared()方法,通知监听器环境已经准备好。
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            //决定是否跳过 BeanInfo 类的扫描
            this.configureIgnoreBeanInfo(environment);
            //只是会向控制台或是日志中输出Spring的Logo和版本信息
            Banner printedBanner = this.printBanner(environment);
            //创建应用程序上下文并加载Bean
            //扩展:在准备好环境之后,接下来要做的就是创建应用程序上下文ApplicationContext对象。ApplicationContext是Spring IoC的核心组件,它不仅是程序所需Bean的容器,还提供了国际化,事件发布等功能。
            //在创建应用程序上下文的时候,首先会根据之前配置决定上下文的具体类型(AnnotationConfigApplicationContext或是AnnotationConfigServletWebServerApplicationContext)。再通过反射实例化到对象。
            context = this.createApplicationContext();
            //为此应用程序上下文设置ApplicationStartup。
            context.setApplicationStartup(this.applicationStartup);
            //准备ApplicationContext
            //扩展:
            //虽然已经得到了ApplicationContext对象,但此时的对象还只是一个空白对象,需要准备和处理后,ApplicationContext才能被使用。
            //在准备过程中主要做了做了几件事:为ApplicationContext设置之前准备好的Environment对象。
			//通过对ApplicationContext后置处理或是BeanDefinitionLoader等方式往容器中添加一些初始的Bean。
			//应用默认的初始化器初始化应用程序上下文(责任链模式的应用,多个初始化器形成一个List,应用程序需要被每个初始化器应用一次,每个初始化器有自己的职责)。
			//准备过程中ApplicationRunListener发出两个消息,分别是contextPrepared和contextLoaded。
            this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            //刷新上下文
            //扩展:(下面在去解析这个方法,比较复杂)
            //通过刷新应用程序上下文发现Bean并加载到容器中。refreshContext()会调用ApplicationContext.refresh()方法。
			//AbstractApplicationContext中定义了refresh()方法的基本框架(模板模式的应用)。
            this.refreshContext(context);
            //在Application完成刷新后,SpringApplication给子类留了afterRefresh()的方法作为回调。
            this.afterRefresh(context, applicationArguments);
            //启动完成后,stopWatch会记录下本次启动消费的时间。
            Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
            if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
            }
            //然后向ApplicationRunListener发布started事件,说明已经启动就绪
            listeners.started(context, timeTakenToStartup);
            //启动完成后,正式运行前,SpringApplication还会执行用户定义的ApplicationRunner和CommandLineRunner两个接口中定义的run()方法。
            this.callRunners(context, applicationArguments);
        } catch (Throwable var12) {this.handleRunFailure(context, var12, listeners);
            throw new IllegalStateException(var12);
        }

        try {//在执行完成后,向ApplicationRunListener发布runing的消息。至此,启动流程结束。
            Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
            listeners.ready(context, timeTakenToReady);
            return context;
        } catch (Throwable var11) {this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var11);
        }
    }
this.refreshContext(context);分析

类:org\springframework\context\support\AbstractApplicationContext.java

@Override
	public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {	StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

			//准备刷新
			//准备刷新的阶段做了初始化和校验的工作。
			prepareRefresh();

			// 告诉子类刷新内部bean工厂
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.(准备用于此上下文的bean工厂。)
			//准备BeanFactory  
			//扩展:
			//对BeanFactory的准备主要是:添加一些必要组件,比如类加载器,表达式解析器,属性编辑器注册表等。
			//以及一些后置处理器,比如ApplicationContextAwareProcessor(xxxAware的接口就是通过后置处理器在Bean创建的时候,通过后置处理器设置的)。此外还有一些特殊的Bean,environment,systemProperties和systemEnvirnoment。
			prepareBeanFactory(beanFactory);

			try {		// (后置处理BeanFactory)Allows post-processing of the bean factory in context subclasses.
				//扩展:
				//对于非WebServlet环境的ApplicationContext而言这个方法是个空方法,但是Web环境下的ApplicationContext会通过这个方法定制一些后处理动作,比如添加WebApplicationContextServletAwareProcessor后置处理器,添加在web环境中可能使用的Scope(session和request)。
				postProcessBeanFactory(beanFactory);

				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				//(在上下文中调用注册为bean的工厂处理器。) Invoke factory processors registered as beans in the context.
				//其实就是实例化并调用BeanFactoryPostProcessor
				//扩展:(看标题有详细解释)
				//BeanFactoryPostProcessor是一种特殊的后置处理器,其操作的对象是针对BeanFactory。
				invokeBeanFactoryPostProcessors(beanFactory);

				// (注册Bean后置处理器)Register bean processors that intercept bean creation.\
				//扩展:
				//上一步是针对BeanFactory和BeanDefinitionRegistry的后置处理器,这一步从BeanFactory中获取针对普通Bean的后置处理器BeanFactoryPostProcessor放到专门的容器beanPostProcessors中。
				registerBeanPostProcessors(beanFactory);
				beanPostProcess.end();

				//(初始化MessageSource处理国际化相关内容) Initialize message source for this context.
				initMessageSource();

				//(初始化ApplicationEventMulticaster) Initialize event multicaster for this context.
				//扩展:
				//ApplicationEventMulticaster是ApplicationEvent广播器,可以通过这个对象向容器中添加移除Listener,也可以通过这个对象发布事件(观察者模式的应用)。
				initApplicationEventMulticaster();

				// (刷新应用程序)Initialize other special beans in specific context subclasses.
				// 扩展:
				//发现了所有的Bean,并且需要实例化的Bean也都被创建好了之后,Spring接下去要做的是创建ThemeSource(和主题相关的组件),以及创建Webserver如果存在。
				//此方法可以重写以添加特定于上下文的刷新工作的模板方法。在实例化单体之前,在初始化特殊bean时调用。
				onRefresh();

				//注册监听器 Check for listener beans and register them.
				//扩展:
				//这一步会将初始化得到的ApplicationListener方法和容器中获得ApplicationListener一起注册到ApplicationEventMulticaster中,并且如果存在需要早起发布的事件,则发布事件。
				registerListeners();

				// (初始化容器中的Bean)Instantiate all remaining (non-lazy-init) singletons.
				//扩展:
				//经过之前的步骤,现在容器中必要的组件都已经准备好了,并且所有需要容器管理的Bean也都已经被发现注册成BeanDefinition注册表中。
				//对于Scope是Singleton的Bean而言,此时已经具备了实例化Bean的条件,因此在这一步中,Spring会对所有Singleton且非lazy-init的Bean进行实例化。
				//主要做法就是获取容器中所有为singletion且非lazyInit的BeanDefinition,然后通过getBean创建出Bean的实例,保存在容器内部。
				finishBeanFactoryInitialization(beanFactory);

				//完成刷新 Last step: publish corresponding event.
				//主要是一些资源清理以及注册LifeCycleProcessor。LifeCycleProcessor可以用来在 Spring 生命周期的refresh和close时触发回调。并且发布Refresh的消息。ContextRefreshedEvent
				finishRefresh();
			}

			catch (BeansException ex) {		if (logger.isWarnEnabled()) {logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				//销毁已创建的单实例以避免悬空资源。Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// 重置“激活”标志。 Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {		// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				//清除缓存
				resetCommonCaches();
				contextRefresh.end();
			}
		}
	}
BeanFactory 详解

BeanFactory是 Spring 框架中容器的底层实现,所有的 Bean 都存放在BeanFactory中,虽然ApplicationContext也实现了BeanFactory接口,但是在其内部还是将获取 Bean 的相关操作委托给内部的DefaultListableBeanFactory变量,只是ApplicationContext帮用户屏蔽了底层的操作,同时提供出一些更符合外部用户使用的接口。

BeanFactoryPostProcessor 详解

BeanFactoryPostProcessor 主要有三个后置处理器,分是:

1、SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor

2、ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor

3、ConfigFileApplicationListener$PropertySourceOrderingPostProcessor。

一般主要是这俩:
在这里插入图片描述
我们看名字也能发现类是怎么来的(外部类是xxxInitializer的就说明是初始化器设置的)。

其中第一个类和启动流程有关,因为它会向容器注册ConfigurationClassPostProcessor。

如果BeanFactoryPostProcessor同时又是BeanDefinitionRegistryPostProcessor,则先进行针对BeanDefinition注册表的后置处理,目的是为了发现Bean。

在最初的三个BeanFactoryProcessor后置处理完成后,会从容器中获取BeanDefinitionRegistryPostProcessor类型的后置处理器(这里主要会得到刚才加载的ConfigurationClassPostProcessor实例)。再调用这些BeanDefinitionRegistry的后置处理器,继续向发现并向容器中注册新的Bean。

这里主要是通过@Configuration注解作为入口发现Bean,如果发现的Bean中又存在新的@ConfigurationBean,则以此Bean为入口再进行发现,直到所有的Bean都被发现。

注意 Bean的发现过程只是向BeanDefinition注册表注册BeanDefinition的过程,并没有针对发现的Bean进行实例化(少部分需要用到的Bean会进行实例化,比如这部分会对BeanDefinitionRegistryPostProcessor类型的Bean实例化)。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

本文标题:【源码分析】Springboot启动流程源码分析-创新互联
文章链接:https://www.cdcxhl.com/article32/dscisc.html

成都网站建设公司_创新互联,为您提供域名注册品牌网站建设定制开发外贸建站建站公司Google

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司