SpringBoot源码分析

目录

1.SpringBoot入口说明

2.SpringBoot的启动流程

3.SpringBoot上下文构建refresh方法解析

4.关键点说明


1.SpringBoot入口说明

SpringBoot工程的入口类是org.springframework.boot.SpringApplication,最主要的有两个方法,一个是构造方法:org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...),一个是run方法:org.springframework.boot.SpringApplication#run(java.lang.String...)。

在构造方法中,初始化了应用启动需要用到的资源,如应用的类型(reactive web/servlet web/jersey),启动引导器bootstrappers(Bootstrapper),上下文初始化对象列表initializers(ApplicationContextInitializer),应用监听器listeners(ApplicationListener)。这里初始化都是通过加载项目依赖的jar文件中的“META-INF/spring.factories”(如spring-boot, spring-boot-autoconfigure等工程中都有该配置,甚至可以自己定义)配置的各个对应类的实现工厂,从org.springframework.boot.SpringApplication#getSpringFactoriesInstances(java.lang.Class<T>) 调用org.springframework.core.io.support.SpringFactoriesLoader#loadSpringFactories获取配置文件中对应的加载配置类的路径信息,通过org.springframework.boot.SpringApplication#createSpringFactoriesInstances方法采用反射的方式,创建接口对应的类实例。在构造方法中,最后通过org.springframework.boot.SpringApplication#deduceMainApplicationClass方法获取主函数类mainApplicationClass。

构造SpringApplication完成之后,会调用run方法执行启动相关流程。这里run方法有两种类型,一种是静态方法,一种是实例变量的成员方法,静态方法内部本质上也是先调用SpringApplication构造方法完成实例初始化,然后再调用实例的run方法完成SpringBoot的启动流程,具体的源码如下。相对而言,自己构造SpringApplication实例,然后调用实例run方法的方式,可自动定制操作优化的空间更多。

//SpringApplication的构造方法
	/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details. The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param resourceLoader the resource loader to use
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #setSources(Set)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
//入参,启动主类
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//根据配置信息,决定是什么类型的web,如servlet web,reactive web等
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
//启动前引导类加载
		this.bootstrappers = new ArrayList<>(getSpringFactoriesInstances(Bootstrapper.class));
//获取配置的初始化加载器
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//获取配置的监听器
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//寻找main方法的入口类
		this.mainApplicationClass = deduceMainApplicationClass();
	}

//SpringApplication的静态run方法
	/**
	 * Static helper that can be used to run a {@link SpringApplication} from the
	 * specified sources using default settings and user supplied arguments.
	 * @param primarySources the primary sources to load
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return the running {@link ApplicationContext}
	 */
	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
//先new一个实例,然后调用实例run方法
		return new SpringApplication(primarySources).run(args);
	}

//根据对应的关键类的存在性,解析获取Web应用的类型
static WebApplicationType deduceFromClasspath() {
//包含 org.springframework.web.reactive.DispatcherHandler
//不包含 org.springframework.web.servlet.DispatcherServlet
//不包含 org.glassfish.jersey.servlet.ServletContainer
		if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
//reactive web application
			return WebApplicationType.REACTIVE;
		}
		for (String className : SERVLET_INDICATOR_CLASSES) {
			if (!ClassUtils.isPresent(className, null)) {
//not run as a web application
				return WebApplicationType.NONE;
			}
		}
//servlet-based web application
		return WebApplicationType.SERVLET;
	}

2.SpringBoot的启动流程

SpringBoot的启动主要在方法org.springframework.boot.SpringApplication#run(java.lang.String...)中,该方法定义了其启动的流程。在该启动流程中,首先初始化监听器listener广播事件,然后创建对应的web应用的环境信息,再构造应用上下文,然后填充上下文内容(这个是关键,org.springframework.boot.SpringApplication#refreshContext方法,下一节分析),创建上下文成功广播创建成功事件,然后调用Runner的勾子执行相应的参数指令,最后广播服务启动成功接受事件处理的事件,到此完成SpringBoot的上下文环境的创建,整个容器构建完成。

/**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return a running {@link ApplicationContext}
	 */
	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
//这里通过bootstrappers的初始化initialize方法初始化上下文创建过程
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
//通过getSpringFactoriesInstances方法从配置文件中读取SpringApplicationRunListener监听实例列表
		SpringApplicationRunListeners listeners = getRunListeners(args);
//针对每一个监听器启动监听,执行SpringApplicationRunListener的starting方法,广播应用启动的事件
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//初始化StandardServletEnvironment(根据web类型获取)上下文配置信息,创建Servlet应用环境
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
//创建上下文context对象,这里SpringBoot的web应用,根据webApplicationType为Servlet推导出
//为基于注解的AnnotationConfigServletWebServerApplicationContext上下文应用,
//这里初始化Bean的定义读取器AnnotatedBeanDefinitionReader和扫描器ClassPathBeanDefinitionScanner,
//并进行加载Bean对象生成上下文,使用的bean生成工厂为org.springframework.beans.factory.support.DefaultListableBeanFactory工厂类
			context = createApplicationContext();
			context.setApplicationStartup(this.applicationStartup);
//初始化上下文内容,设置ConfigurableListableBeanFactory实例(org.springframework.beans.factory.support.DefaultListableBeanFactory对象)
//相关配置信息,根据入参args初始化上下文信息
			prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
//调用refresh模版方法构建完整的上下文org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#refresh方法构建context上下文
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
//上下文构建完成,调用时间监听器started方法,发布服务运行状态事件
			listeners.started(context);
//调用Runner勾子,org.springframework.boot.ApplicationRunner,
//org.springframework.boot.CommandLineRunner两种服务实现类,对上下文入参进行相关处理
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, listeners);
			throw new IllegalStateException(ex);
		}

		try {
//上下文创建成功,服务启动之后发布接受事件监听的事件,标示服务完整的启动
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

3.SpringBoot上下文构建refresh方法解析

SpringBoot上下文构建通过org.springframework.boot.SpringApplication#refreshContext方法进入,通过层层调用,最终落实到org.springframework.context.ConfigurableApplicationContext#refresh方法,在这里我们以Servlet Web应用为主,调用到org.springframework.context.support.AbstractApplicationContext#refresh方法,该方法里面以模板方法,一步步构建完整的context上下文,具体源码说明如下:

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

//初始化加载properties配置并校验配置信息,准备加载环境信息
			// Prepare this context for refreshing.
			prepareRefresh();

//重置BeanFactory对象
			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

//配置BeanFactory上下文内容,注册依赖以及非依赖的接口,注册单利环境信息
			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
//对BeanFactory进行包含注册各种Scope信息,以及Servlet上下文信息注册
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
//扫描注解信息,在上下文中创建Bean信息,
//调用org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions
//方法进行解析和校验,通过org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader#loadBeanDefinitions加载Bean,
//调用org.springframework.beans.factory.support.DefaultListableBeanFactory#registerBeanDefinition方法,
//最后Bean对象存储到org.springframework.beans.factory.support.DefaultListableBeanFactory#beanDefinitionMap变量中
//(即IOC的容器是这个MAP存储的)
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

//创建Bean之后,添加BeanFactory的后置处理器
				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);
				beanPostProcess.end();

//构造MessageSource类的Bean对象,注册为messageSource单利范围
				// Initialize message source for this context.
				initMessageSource();

//初始化ApplicationEventMulticaster类的Bean对象,注册为applicationEventMulticaster单利对象,应用事件多播
				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

//创建WebServer,获取ServletWebServerFactory类对象,调用getWebServer方法创建Server服务,如创建Jetty/Tomcat/UndertowServletWeb等,并注册Shutdown/Stop等处理单利类
				// Initialize other special beans in specific context subclasses.
				onRefresh();

//注册广播事件监听器ApplicationListener
				// Check for listener beans and register them.
				registerListeners();

//实例化所有非延迟加载的单利实例
				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

//注册WebServer的生命管理类lifecycleProcessor单利,
//并启动相应的hook应用,然后启动webServer(调用this.webServer.start()方法启动),
//并广播ServletWebServerInitializedEvent事件通知webServer初始化完成事件
				// Last step: publish corresponding event.
				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();
			}
		}
	}

4.关键点说明

1) Spring IOC容器为org.springframework.beans.factory.support.DefaultListableBeanFactory#beanDefinitionMap变量,改变量是ConcurrentHashMap类型,用于存放实例化的容器。

2) Bean对象是由org.springframework.beans.factory.config.BeanDefinitionHolder类型承载,记录了beanName,别名aliases,以及Spring的BeanDefinition对象(即实例)。

3)BeanFactory(The root interface for accessing a Spring bean container.This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name.)的默认实现为org.springframework.beans.factory.support.DefaultListableBeanFactory,在refresh方法的起始阶段创建。关于FactoryBean是作为一个工厂的Bean对象待实现的interface,不同于普通的Bean 对象,具体说明:If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.

4)ConfigurableApplicationContext类,Servlet web应用是AnnotationConfigServletWebServerApplicationContext类实例,初始化了整个Spring的上下文应用。

5)Servlet Web应用是创建好应用的Context,以及相应的容器初始化完成之后,创建内嵌的Web Server(如tomcat)并启动Server,然后对外提供服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值