共计 9267 个字符,预计需要花费 24 分钟才能阅读完成。
众所周知,类上面带有 @SpringBootApplication 注解的类,即为 springboot 的启动类。一个 springboot 项目只能有一个启动类。我们来分析一下 SpringBoot 项目的启动过程,首先看看启动类里面都包含什么
@SpringBootApplication
public class HelloWorldMainApplication {public static void main(String[] args) {
//spring 应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
从上面的代码中可以看出真正起作用的是 SpringApplication.run(); 这个方法,下面主要分析一下这个方法。
一、实例化 SpringApplication
SpringApplication 初始化时主要做三件事情:
- 1. 根据 classpath 下是否存在 (ConfigurableWebApplicationContext) 判断是否要启动一个 web applicationContext
- 2.SpringFactoriesInstances 加载 classpath 下所有可用的 ApplicationContextInitializer
- 3.SpringFactoriesInstances 加载 classpath 下所有可用的 ApplicationListener
/**
* 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));
//1. 根据 classpath 下是否存在 (ConfigurableWebApplicationContext) 判断是否要启动一个 web applicationContext
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//2.SpringFactoriesInstances 加载 classpath 下所有可用的 ApplicationContextInitializer
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//3.SpringFactoriesInstances 加载 classpath 下所有可用的 ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();}
二、实例化完成后调用 run()方法
调用 run()方法执行的过程主要分为以下几步:
- 1. 遍历 SpringApplication 初始化过程中加载的 SpringApplicationRunListeners
- 2. 调用 Starting()监听 SpringApplication 的启动
- 3. 加载 SpringBoot 配置环境(ConfigurableEnvironment)
- 4. 设置 banner 属性
- 5. 创建 ConfigurableApplicationContext(应用配置上下文)
- 6. 将 listeners、environment、applicationArguments、bannner 等重要组件与上下文对象关联
- 7.bean 的实力化完成
/**
* 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();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
//1. 遍历 SpringApplication 初始化过程中加载的 SpringApplicationRunListeners
SpringApplicationRunListeners listeners = getRunListeners(args);
//2. 调用 starting()监听 SpringApplication 的启动
listeners.starting();
try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//3. 加载 SpringBoot 配置环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
//4. 设置 banner 属性
Banner printedBanner = printBanner(environment);
//5. 创建 ConfigurableApplicationContext(应用配置上下文)
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class}, context);
//6. 将 listeners、environment、applicationArguments、banner 等重要组件与上下文对象关联
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//7. 实例化 bean
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;
}
1. 遍历 SpringApplication 初始化过程中加载的 SpringApplicationRunListeners
private SpringApplicationRunListeners getRunListeners(String[] args) {Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
return new SpringApplicationRunListeners(logger,
getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}
2. 调用 Starting()监听 SpringApplication 的启动
public void starting() {// 遍历所有的 SpringApplicationRunListener,调用 starting()方法监听 SpringApplication 的启动
for (SpringApplicationRunListener listener : this.listeners) {listener.starting();
}
}
3. 加载 SpringBoot 配置环境(ConfigurableEnvironment)
加载 SpringBoot 配置环境(configurableEnvironment),如果是通过 web 容器发布,会加载 StandardEnvironment。将配置文件 (Environment) 加入到监听器对象中(SpringApplicationRunListeners)
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
// 如果 environment 不为空直接返回 || 如果是 web 环境则直接实例化 StandardServletEnvironment 类 || 如果不是 web 环境则直接实例化 StandardEnvironment 类
ConfigurableEnvironment environment = getOrCreateEnvironment();
// 配置环境信息
configureEnvironment(environment, applicationArguments.getSourceArgs());
// 通知所有的监听者,环境已经准备好了
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
4. 设置 banner 属性
private Banner printBanner(ConfigurableEnvironment environment) {
// 如果未开启 banner 打印直接返回
if (this.bannerMode == Banner.Mode.OFF) {return null;}
// 创建 ResourceLoader 对象
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
: new DefaultResourceLoader(getClassLoader());
// 创建 SpringApplicationBannerPrinter,该对象用来打印 banner
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
// 如果 bannerMode 模式为 LOG,则将 bannner 打印到 log 文件中
if (this.bannerMode == Mode.LOG) {return bannerPrinter.print(environment, this.mainApplicationClass, logger);
}
// 打印 banner 到控制台
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
5. 初始化 ConfigurableApplicationContext(应用配置上下文)
在 SpringBoot 中,应用类型分为三类
public enum WebApplicationType {
/**
* The application should not run as a web application and should not start an
* embedded web server.
*/
// 应用程序不是 web 应用,也不应该用 web 服务器去启动
NONE,
/**
* The application should run as a servlet-based web application and should start an
* embedded servlet web server.
*/
// 应用程序应作为基于 servlet 的 web 应用程序运行,并应启动嵌入式 servlet web(tomcat)服务器
SERVLET,
/**
* The application should run as a reactive web application and should start an
* embedded reactive web server.
*/
// 应用程序应作为 reactive web 应用程序运行,并应启动嵌入式 reactive web 服务器。REACTIVE;
}
根据 webEnvironment 是否是 web 环境创建默认的 contextClass,AnnotationConfigEnbeddedWebApplicationContext(通过扫描所有注解类来加载 bean)和 ConfigurableWebApplicationContext), 最后通过 BeanUtils 实例化上下文对象,并返回。
/**
* Strategy method used to create the {@link ApplicationContext}. By default this
* method will respect any explicitly set application context or application context
* class before falling back to a suitable default.
* @return the application context (not yet refreshed)
* @see #setApplicationContextClass(Class)
*/
protected ConfigurableApplicationContext createApplicationContext() {
// 根据 webEnvironment 是否是 web 环境创建默认的 contextClass
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {switch (this.webApplicationType) {
case SERVLET:
//AnnotationConfigServletWebServerApplicationContext
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
//AnnotationConfigReactiveWebServerApplicationContext
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
//AnnotationConfigApplicationContext
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext," + "please specify an ApplicationContextClass",
ex);
}
}
//BeanUtils 实例化上下文对象
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
6. 将 listeners、environment、applicationArguments、banner 等重要组件与上下文对象关联
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
// 设置上下文的 environment
context.setEnvironment(environment);
// 应用上下文后处理
postProcessApplicationContext(context);
// 在 context refresh 之前,对其应用 ApplicationContextInitializer
applyInitializers(context);
// 上下文准备
listeners.contextPrepared(context);
// 打印启动日志和启动应用的 profile
if (this.logStartupInfo) {logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
// 向 beanFactory 注册单例 bean:命令行参数 bean
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
// 向 beanFactory 注册单例 bean:banner bean
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// Load the sources
// 获取 SpringApplication 的 primarySources 属性
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
// 将 bean 加载到应用上下文
load(context, sources.toArray(new Object[0]));
// 向上下文添加 ApplicationListener,并广播 ApplicationPreparedEvent 事件
listeners.contextLoaded(context);
}
7.bean 的实例化完成,刷新应用上下文
正文完
发表至: java
2019-08-09