springboot启动过程(1)-初始化
1 springboot启动时,只需要调用一个类前面加了@SpringBootApplication的main函数,执行SpringApplication.run(DemoApplication.class, args)即可,这里初始化了一个SpringApplication实例,然后调用run启动springboot。run方法中,调用了initialize,如下
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initialize(Object[] sources) {
//把传入的source设置到SpringApplication的变量中
if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources));
}
// 如果是web程序,设置web环境。web程序必须让类加载器存在servlet 和 ConfigurableWebApplicationContext
this.webEnvironment = deduceWebEnvironment();
//找到ApplicationContextInitializer类实例化为成员变量,这个过程是找到所有的初始化器。
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//把ApplicationListener实例化为变量,就是找到所有的事件监听器。
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//找到入口类,即放置main函数的类。
this.mainApplicationClass = deduceMainApplicationClass();
}
(1)初始化context。 上面setInitializers 得到了ApplicationContextInitializer类型对象的集合,ApplicationContextInitializer是一个可以用来初始化各种ApplicationContex的接口,就一个initialize方法,上面初始化的时候用到了getSpringFactoriesInstances方法如下,
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<String>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
可以看到,方法用oadFactoryNames(type, classLoader));通过类型得到所有spring factories的名字,然后根据名字使用createSpringFactoriesInstances创造出实例对象,最后把创建好的对象排序并返回。springfactory是spring-boot-xxx(版本)包里面的META-INF/spring.factories如下 ,然后通过createSpringFactoriesInstances方法利用反射创造出这些实例,实例共四个都是ApplicationContextInitializer的实现类,作用也如下。
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
//为ApplicationContext添加一个能够检查配置,在错误配置时能够打印报错信息的BeanFactoryPostProcessor
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
//为ApplicationContext设置id
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
//从ApplicationContext的环境配置中,读取Initializer并使用
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
//为ApplicationContext设置一个环境变量,方便使用服务器正在监听的端口号。
org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer
(2)初始化监听器 上文中setListeners进行监听器初始化操作,跟初始化context一样,根据类型ApplicationListener.class从spring.factories里找到各种监听器,applicationListener就一个方法onApplicationEvent,用于监听各种事件。
# 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.liquibase.LiquibaseServiceLocatorApplicationListener,\
org.springframework.boot.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.logging.LoggingApplicationListener
* applicationContext 事件机制 spring的事件机制基于观察者模式,与所有的事件机制都基本类似,他们都需要 事件源, 事件 和事件监听器 组成。只是此处的事件源是ApplicationContext。包括两个重要部分,
ApplicationEvent: 容器事件,必须由 ApplicationContext发布。
ApplicationListener: 监听器,可有容器中的任何监听器Bean担任。
我们使用时可以定一个event继承ApplicationEvent,然后定一个listener继承ApplicationListener,实现onApplicationEvent方法,在方法里可以根据事件类型判断事件,进行响应处理。事件发布的时候,需要得到ApplicationContext,调用publishEvent(event)进行发布即可。spring也内置了几个事件。
(3)上文的最后,deduceMainApplicationClass方法中,通过获取当前方法调用栈,得到main函数的类,赋值给成员变量。
springboot启动过程(1)-初始化的更多相关文章
- SpringBoot启动过程原理
最近这两年springboot突然火起来了,那么我们就来看看springboot的运行原理. 一.springboot的三种启动方式: 1.运行带有main方法的2.通过命令 Java -jar命令3 ...
- Spring Boot 学习笔记一(SpringBoot启动过程)
SpringBoot启动 Spring Boot通常有一个名为*Application的入口类,在入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法. 在main方法 ...
- (四)SpringBoot启动过程的分析-预处理ApplicationContext
-- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇(三)SpringBoot启动过程的分析-创建应用程序上下文,本文将分析上下文创建完毕之后的下一步操作:预处理上下文容器. 预处理上下文 ...
- (三)SpringBoot启动过程的分析-创建应用程序上下文
-- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇(二)SpringBoot启动过程的分析-环境信息准备,本文将分析环境准备完毕之后的下一步操作:ApplicationContext的创 ...
- (一)SpringBoot启动过程的分析-启动流程概览
-- 以下内容均基于2.1.8.RELEASE版本 通过粗粒度的分析SpringBoot启动过程中执行的主要操作,可以很容易划分它的大流程,每个流程只关注重要操作为后续深入学习建立一个大纲. 官方示例 ...
- (五)SpringBoot启动过程的分析-刷新ApplicationContext
-- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇[(四)SpringBoot启动过程的分析-预处理ApplicationContext] (https://www.cnblogs.co ...
- SpringBoot启动过程原理(转)
1.1 Springboot启动: @SpringBootApplication public class ServerApplication { public static void main(St ...
- springboot启动过程
使用了很长时间的springboot了,一直都知道它简单易用,简化了框架的搭建过程,但是还是不知道它是如何启动的,今天就跟着springboot的源码,去探探这其中的奥妙 以下是spring应用的启动 ...
- (二)SpringBoot启动过程的分析-环境信息准备
-- 以下内容均基于2.1.8.RELEASE版本 由上一篇SpringBoot基本启动过程的分析可以发现在run方法内部启动SpringBoot应用时采用多个步骤来实现,本文记录启动的第二个环节:环 ...
随机推荐
- Windows环境下CGAL的安装
1 准备工作 下载cmake 下载CGAL安装包 学习如何设置环境变量 安装Qt运行demos. libQGLViewer用来运行 3D CGAL demos. 确定Visual Studio 相应的 ...
- win32下开发hadoop
转载自:http://my.oschina.net/muou/blog/408543[木偶:Windows下使用Hadoop2.6.0-eclipse-plugin插件] 对于一些细节地 ...
- 用lsmod看硬盘驱动决定是sata还是scsi盘
sas盘 scsi盘 sata盘都是显示为sdx的所以无法区别唯一可以分别的是看看内核加载的驱动模块有啥 lsmod....mptsas 62545 7
- LeetCode OJ:Balanced Binary Tree(平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- python虚拟开发环境搭建(virtualenv和virtualenvwrapper)
虚拟开发环境的搭建 (0) 搭建虚拟环境的意义 使不同的开发环境独立 环境升级不影响其他开发环境,也不影响全局 防止包管理的混乱 (1) 指定 虚拟环境的创建目录 环境变量设置 创建 WORKON_H ...
- python 字符串大小写相关函数
改写:(都不会改变原字符串) s = 'hEllo wOrld' s Out[3]: 'hEllo wOrld' s.upper()#全部大写 Out[4]: 'HELLO WORLD' s Out[ ...
- LeetCode Repeated String Match
原题链接在这里:https://leetcode.com/problems/repeated-string-match/description/ 题目: Given two strings A and ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
- 尚硅谷Java视频教程导航(学习路线图)
最近很火,上去看了看,对于入门的人还是有点作用的,做个记号,留着以后学习. Java视频教程下载导航(学习路线图) 网站地址:http://www.atguigu.com/download.shtml
- bzoj 3625(CF 438E)The Child and Binary Tree——多项式开方
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3625 http://codeforces.com/contest/438/problem/E ...