SpringBoot系列文章简介

SpringBoot源码阅读辅助篇:

  Spring IoC容器与应用上下文的设计与实现 <https://www.cnblogs.com/hello-shf/p/11006750.html>

SpringBoot启动流程源码分析:

* SpringBoot启动流程分析(一):SpringApplication类初始化过程
<https://www.cnblogs.com/hello-shf/p/10976646.html>
* SpringBoot启动流程分析(二):SpringApplication的run方法
<https://www.cnblogs.com/hello-shf/p/10992377.html>
* SpringBoot启动流程分析(三):SpringApplication的run方法之prepareContext()方法
<https://www.cnblogs.com/hello-shf/p/11018403.html>
* SpringBoot启动流程分析(四):IoC容器的初始化过程
<https://www.cnblogs.com/hello-shf/p/11051476.html>
* SpringBoot启动流程分析(五):SpringBoot自动装配原理实现
<https://www.cnblogs.com/hello-shf/p/11057861.html>
* SpringBoot启动流程分析(六):IoC容器依赖注入
<https://www.cnblogs.com/hello-shf/p/11060546.html>
笔者注释版Spring Framework与SpringBoot源码git传送门:请不要吝啬小星星

* spring-framework-5.0.8.RELEASE
<https://github.com/hello-shf/spring-framework-5.0.8.RELEASE.git>
* SpringBoot-2.0.4.RELEASE
<https://github.com/hello-shf/spring-boot-build.git>
一、SpringApplication初始化过程

  1.1、SpringBoot项目的mian函数

  常规的这个主类如下图所示,我们一般会这样去写。

 

  在这个类中需要关注的是

* @SpringBootApplication
* SpringApplication.run()
  关于 @SpringBootApplication 注解,在后面分析SpringBoot自动装配的章节会展开去分析。

  本章节中我们需要关注的就是 SpringApplication.run() 方法。

  查看run()方法的实现,如下面代码所示,我们发现其实其首先是创建了 SpringApplication 的实例,然后调用了 
SpringApplication 的run()方法,那本章我们关注的就是 SpringApplication 创建实例的过程。
/** * 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) {return new
SpringApplication(primarySources).run(args); }
   

  1.2、 SpringApplication() 构造方法

  继续查看源码, SpringApplication 实例化过程,首先是进入但参数的构造方法,最终回来到两个参数的构造方法。
1 public SpringApplication(Class<?>... primarySources) { 2 this(null,
primarySources); 3 } 4 5 @SuppressWarnings({"unchecked", "rawtypes"}) 6
public SpringApplication(ResourceLoader resourceLoader, Class<?>...
primarySources) { 7 this.resourceLoader = resourceLoader; 8
Assert.notNull(primarySources, "PrimarySources must not be null"); 9 this
.primarySources =new LinkedHashSet<>(Arrays.asList(primarySources)); 10 //
推断应用类型,后面会根据类型初始化对应的环境。常用的一般都是servlet环境 11 this.webApplicationType =
deduceWebApplicationType();//2.2.112 //初始化classpath下
META-INF/spring.factories中已配置的ApplicationContextInitializer 13
setInitializers((Collection)
getSpringFactoriesInstances(ApplicationContextInitializer.class));//2.2.2 14 //
初始化classpath下所有已配置的 ApplicationListener 15 setListeners((Collection)
getSpringFactoriesInstances(ApplicationListener.class));//2.2.3 16 //根据调用栈,推断出
main 方法的类名 17 this.mainApplicationClass = deduceMainApplicationClass(); 18 }
 

   1.2.1、deduceWebApplicationType();该方法推断应用的类型。 SERVLET REACTIVE NONE 
1 //常量值 2 private static final String[] WEB_ENVIRONMENT_CLASSES =
{"javax.servlet.Servlet", 3
"org.springframework.web.context.ConfigurableWebApplicationContext"}; 4 5
private static final String REACTIVE_WEB_ENVIRONMENT_CLASS =
"org.springframework." 6 + "web.reactive.DispatcherHandler"; 7 8 private
static final String MVC_WEB_ENVIRONMENT_CLASS = "org.springframework." 9 +
"web.servlet.DispatcherServlet"; 10 11 private static final String
JERSEY_WEB_ENVIRONMENT_CLASS = "org.glassfish.jersey.server.ResourceConfig"; 12
13 /** 14 * 判断 应用的类型 15 * NONE: 应用程序不是web应用,也不应该用web服务器去启动 16 * SERVLET:
应用程序应作为基于servlet的web应用程序运行,并应启动嵌入式servlet web(tomcat)服务器。17 * REACTIVE:
应用程序应作为 reactive web应用程序运行,并应启动嵌入式 reactive web服务器。18 * @return 19 */ 20
private WebApplicationType deduceWebApplicationType() { 21 //
classpath下必须存在org.springframework.web.reactive.DispatcherHandler 22 if
(ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS,null) 23 &&
!ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS,null) 24 &&
!ClassUtils.isPresent(JERSEY_WEB_ENVIRONMENT_CLASS,null)) { 25 return
WebApplicationType.REACTIVE;26 } 27 for (String className :
WEB_ENVIRONMENT_CLASSES) {28 if (!ClassUtils.isPresent(className, null)) { 29
return WebApplicationType.NONE; 30 } 31 } 32 //
classpath环境下存在javax.servlet.Servlet或者org.springframework.web.context.ConfigurableWebApplicationContext
33 return WebApplicationType.SERVLET; 34 }
  返回类型是WebApplicationType的枚举类型, WebApplicationType 有三个枚举,三个枚举的解释如其中注释

  具体的判断逻辑如下:

*
WebApplicationType.REACTIVE 
classpath下存在org.springframework.web.reactive.DispatcherHandler

*
WebApplicationType.SERVLET
classpath下存在javax.servlet.Servlet或者org.springframework.web.context.ConfigurableWebApplicationContext

*
WebApplicationType.NONE 不满足以上条件。

  

  1.2.2、 setInitializers((Collection)
getSpringFactoriesInstances(ApplicationContextInitializer.class)); 

  初始化classpath下 META-INF/spring.factories中已配置的ApplicationContextInitializer。
1 private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) { 2
return getSpringFactoriesInstances(type, new Class<?>[]{}); 3 } 4 5 /** 6
* 通过指定的classloader 从META-INF/spring.factories获取指定的Spring的工厂实例 7 * @param type
8 * @param parameterTypes 9 * @param args 10 * @param <T> 11 * @return 12
*/ 13 private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, 14
Class<?>[] parameterTypes, Object... args) { 15 ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();16 // Use names and ensure
unique to protect against duplicates17 //通过指定的classLoader从
META-INF/spring.factories 的资源文件中,18 //读取 key 为 type.getName() 的 value 19
Set<String> names =new LinkedHashSet<>
(SpringFactoriesLoader.loadFactoryNames(type, classLoader));20 //创建Spring工厂实例 21
List<T> instances = createSpringFactoriesInstances(type, parameterTypes, 22
classLoader, args, names);23 //
对Spring工厂实例排序(org.springframework.core.annotation.Order注解指定的顺序) 24
AnnotationAwareOrderComparator.sort(instances);25 return instances; 26 }
 

  看看 getSpringFactoriesInstances 都干了什么,看源码,有一个方法很重要 loadFactoryNames()
 这个方法很重要,这个方法是spring-core中提供的从META-INF/spring.factories中获取指定的类(key)的同一入口方法。

在这里,获取的是key为 org.springframework.context.ApplicationContextInitializer 的类。

  debug看看都获取到了哪些



 

  上面说了,是从classpath下 META-INF/spring.factories中获取,我们验证一下:





  发现在上图所示的两个工程中找到了debug中看到的6条结果。 ApplicationContextInitializer 是Spring框架的类,
这个类的主要目的就是在   ConfigurableApplicationContext
 调用refresh()方法之前,回调这个类的initialize方法。通过  ConfigurableApplicationContext
 的实例获取容器的环境Environment,从而实现对配置文件的修改完善等工作。

  关于怎么实现自定义的 ApplicationContextInitializer 请看我的另一篇专门介绍该类的博客
<https://www.cnblogs.com/hello-shf/p/10987360.html>。

 

  1.2.3、 setListeners((Collection)
getSpringFactoriesInstances(ApplicationListener.class)); 

  初始化classpath下 META-INF/spring.factories中已配置的 ApplicationListener。

   ApplicationListener 的加载过程和上面的 ApplicationContextInitializer
 类的加载过程是一样的。不多说了,至于 ApplicationListener 是spring的事件监听器,典型的观察者模式,通过 
ApplicationEvent 类和 ApplicationListener
 接口,可以实现对spring容器全生命周期的监听,当然也可以自定义监听事件。为了梳理springboot的启动流程在这里先不说这个了。后面有时间的话再介绍。

   关于ApplicationContextInitializer的详细介绍请看<
SpringBoot之ApplicationContextInitializer的理解和使用
<https://www.cnblogs.com/hello-shf/p/10987360.html>>

 二、总结

  关于 SpringApplication 类的构造过程,到这里我们就梳理完了。纵观 SpringApplication
 类的实例化过程,我们可以看到,合理的利用该类,我们能在spring容器创建之前做一些预备工作,和定制化的需求。

比如,自定义SpringBoot的Banner,比如自定义事件监听器,再比如在容器refresh之前通过自定义 
ApplicationContextInitializer 修改配置一些配置或者获取指定的bean都是可以的。。。

  下一节开始分析SpringBoot容器的构建过程,也就是那个大家多少都看过的run();方法。

   

  原创不易,转载请注明出处。

  如有错误的地方还请留言指正。

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信