SpringApplication是所有springboot的入口类,分析此类有助于我们了解springboot的工作机制。本文以2.0.3.REALEASE版本作分析

SpringApplication

调用实例如下

package com.example.demospringbootweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoSpringbootWebApplication { public static void main(String[] args) {
SpringApplication.run(DemoSpringbootWebApplication.class, args);
}
}

调用的是SpringApplication.run()方法进行应用程序的启动。代码很简单也容易让用户上手,笔者这就进入其具体的类以探瑰宝。

注释描述

先看下其官方注释,有助于我们入门。由于注释过长,笔者此处只对其主要内容作下翻译总结

  1. 可以简单的通过main()函数来辅助启动一个spring应用程序。默认情况下其会按照以下步骤来辅助我们创建的应用

    • 创建一个关联的ApplicationContext实例
    • 注册CommandLinePropertySource实例暴露命令行的参数作为spring的属性
    • 刷新ApplicationContext,并加载所有的单例beans
    • 触发实现了CommandLineRunner的实例beans
  2. SpringApplications可以读取来自不同源的beans。官方建议用户使用@Configuration注解相应的启动类,当然也支持从以下方式加载相应的beans

    • AnnotatedBeanDefinitionReader加载指定的类
    • XmlBeanDefinitionReader加载XML的配置信息或者GroovyBeanDefinitionReader加载groovy脚本资源
    • ClassPathBeanDefinitionScanner扫描指定的包加载相应bean

过于抽象,笔者继续通过源码来对上述的内容进行回顾

构造函数

	/**
* 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环境
this.webApplicationType = deduceWebApplicationType();
// 加载ApplicationContextInitializer接口类
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
// 加载ApplicationListener接口类
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 推断主函数类
this.mainApplicationClass = deduceMainApplicationClass();
}

对上述的注释作下简单的解释

SpringApplication#deduceWebApplicationType()

推断是否为web环境,源码如下

	private WebApplicationType deduceWebApplicationType() {
if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
for (String className : WEB_ENVIRONMENT_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}
return WebApplicationType.SERVLET;
}

从代码层看总共有三种应用类型,也代表了三个环境类型

  • WebApplicationType.REACTIVE reactive web应用(classpath环境下须有org.springframework.web.reactive.DispatcherHandler)
  • WebApplicationType.SERVLET servlet web应用(classpath环境下存在javax.servlet.Servlet或者org.springframework.web.context.ConfigurableWebApplicationContext)
  • WebApplicationType.NONE 简单的JAVA应用(classpath环境不存在上述的类)

SpringApplication#deduceMainApplicationClass()

推断主函数类,源码如下

	private Class<?> deduceMainApplicationClass() {
try {
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if ("main".equals(stackTraceElement.getMethodName())) {
return Class.forName(stackTraceElement.getClassName());
}
}
}
catch (ClassNotFoundException ex) {
// Swallow and continue
}
return null;
}

很简单,就是寻找哪个类下含有main方法,此处和我们常用的启动类不谋而合

SpringApplication#getSpringFactoriesInstances()

找寻相应的接口实现类,源码如下

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
Class<?>[] parameterTypes, Object... args) {
// 上下文classLoader
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// 通过SpringFactoriesLoader来加载相应的类
Set<String> names = new LinkedHashSet<>(
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}

进而查看相应的静态方法SpringFactoriesLoader.loadFactoryNames(),源码如下

	public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
// 关键处理类
return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}

关键处理类出来了,源码跟上

	private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
// 缓存处理
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
} try {
// 找寻所有classpath下的"META-INF/spring.factories"文件
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
// 对含有,的进行分隔并转为list集合
List<String> factoryClassNames = Arrays.asList(
StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
result.addAll((String) entry.getKey(), factoryClassNames);
}
}
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}

由此我们得出结论,classpath环境下所有含META-INF/spring.factories的文件,里面约定了默认的实现。笔者以spring-boot-2.0.3.REALEASE.jar为例

# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader # Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener # Error Reporters
org.springframework.boot.SpringBootExceptionReporter=\
org.springframework.boot.diagnostics.FailureAnalyzers # Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer # Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener # Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor # Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BindValidationFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.UnboundConfigurationPropertyFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer # FailureAnalysisReporters
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter

因此SpringApplication构造函数中加载的ApplicationContextInitializer类有如下

  • ConfigurationWarningsApplicationContextInitializer (对ComponentScan指定的值为"org"等进行报警输出)
  • ContextIdApplicationContextInitializer (创建默认名为application的ContextId对象,也可通过spring.application.name指定)
  • DelegatingApplicationContextInitializer (对context.initializer.classes指定的class集合进行加载)
  • ServerPortInfoApplicationContextInitializer (将local.server.port设置为指定的web端口,默认为8080)


而加载的ApplicationListener类有如下

  • ClearCachesApplicationListener (反射工具缓存清空事件)
  • ParentContextCloserApplicationListener (父ApplicationContext关闭事件)
  • FileEncodingApplicationListener (系统变量配置的file.encoding值是否与环境变量spring.mandatory-file-encoding一致事件)
  • AnsiOutputApplicationListener (控制台彩色输出事件,可通过spring.output.ansi.enabled来指定)
  • ConfigFileApplicationListener (读取spring.profile.active/spring.profile.include配置)
  • DelegatingApplicationListener (委托事件处理类)
  • ClasspathLoggingApplicationListener (打印classpath信息,级别为debug)
  • LoggingApplicationListener (日志处理事件)
  • LiquibaseServiceLocatorApplicationListener (classpath是否存在liquibase的CustomResolverServiceLocator类判断事件)

其中ApplicationListener所绑定事件的触发顺序小结如下

1.ApplicationStartingEvent[应用程序启动事件starting]

2.ApplicationEnvironmentPreparedEvent[环境变量配置事件environmentPrepared]

3.ApplicationPreparedEvent[spring上下文加载前事件contextPrepared]

4.ApplicationStartedEvent[spring上下文加载完毕事件contextLoaded]此事件之后会统一调用ApplicationRunner/CommandLineRunner的实现类

5.ApplicationReadyEvent[应用准备事件running]

6.ApplicationFailedEvent/ContextClosedEvent[抛异常或者启动失败调用]

小结

由此SpringApplication构造函数完成了一些必要的初始化,重点在于ApplicationContextInitializerApplicationListener接口类。并且通过构造函数反射来进行实例化

限于篇幅过长,笔者将对SpringApplication#run()方法的具体解析放于下一章节来分析

springboot情操陶冶-SpringApplication(一)的更多相关文章

  1. springboot情操陶冶-SpringApplication(二)

    承接前文springboot情操陶冶-SpringApplication(一),本文将对run()方法作下详细的解析 SpringApplication#run() main函数经常调用的run()方 ...

  2. springboot情操陶冶-@Configuration注解解析

    承接前文springboot情操陶冶-SpringApplication(二),本文将在前文的基础上分析下@Configuration注解是如何一步一步被解析的 @Configuration 如果要了 ...

  3. springboot情操陶冶-web配置(一)

    承接前文springboot情操陶冶-@SpringBootApplication注解解析,在前文讲解的基础上依次看下web方面的相关配置 环境包依赖 在pom.xml文件中引入web依赖,炒鸡简单, ...

  4. springboot情操陶冶-@ConfigurationProperties注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用 @ConfigurationProper ...

  5. springboot情操陶冶-web配置(九)

    承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑 WebSecurityConfigurerAdapter 在剖析WebSecur ...

  6. springboot情操陶冶-web配置(七)

    参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...

  7. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

  8. springboot情操陶冶-web配置(二)

    承接前文springboot情操陶冶-web配置(一),在分析mvc的配置之前先了解下其默认的错误界面是如何显示的 404界面 springboot有个比较有趣的配置server.error.whit ...

  9. springboot情操陶冶-web配置(三)

    承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...

随机推荐

  1. Win7 VS2017编译Blender2.79

    去年在VS2013环境编译过一次,重装系统后换了VS2017,正好刚编译完Godot3.0.2,顺手把Blender也编译了吧. 官方Windows下编译指南 https://wiki.blender ...

  2. Web Service CXF的工作流程

    我们一起走进系统的内部,跟随每一个调用,去透视系统的每一个层面. 一.我们定义整个目录都在CXFServlet的监控之下 <servlet> <servlet-name>CXF ...

  3. Geomystery现已上架!

    欢迎体验由王涵.程立智.郑昊.蔡镇泽.李明伦.温志成同学开发的几何解谜游戏Geomystery,现已上架Microsoft官方商城! https://www.microsoft.com/zh-cn/s ...

  4. C pointer again …

    记录一个比较基础的东东…… C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

  5. python 初级重点

    关于python初学时遇到的重点: 1 python 2 和3 的区别 python2**不识别中文** -*- coding: utf-8 -*-(因为不能识别中文,所以代码有中文时需要在最前面加入 ...

  6. c#项目减少源代码大小

    这次的代码缩减主要通过了这几个方面 1.bin和obj文件的删除(以前真的不知道,只是通过右键属性发现这些文件太大,然后上网搜索才知道,这些文件在源代码备份的时候是建议删掉的) 删掉的好处: 1.减少 ...

  7. FPGA定点小数计算中截位形式的探讨

    在FPGA设计过程中难免会碰到需要进行截位,那定点小数的计算过程中我们需要注意些什么呢? 首先,我们考虑如下计算式. sin cos 数据形式是 FIX_32_30 X Y Z 数据形式是 FIX_3 ...

  8. shell 命令 grep -R 查询包含指定内容的文件

    grep -R 举个栗子,在有上百个sql文件的目录下,查找使用 spark引擎 执行的文件. 代码是: grep -R spark ./* 返回的就是包含  spark 的sql文件名.

  9. Flask框架(一)

    from flask import Flask app = Flask(__name__) @app.route('/') def index(): return '<h1>hello w ...

  10. Spring5中的DispatcherServlet初始化

    Spring MVC像许多其它Web框架,被设计围绕前端控制器(DispatcherServlet)实际的工作是由可配置的,委托组件执行提供了一种用于请求处理的共享算法.这个模型是灵活的,支持不同的工 ...