springboot 源码笔记
1.springAppication构造器
基于getSpringFactoriesInstances方法构造如下类(获取文件内容在META-INF/spring.factories文件中)
1.1 初始化ApplicationContextInitializer.class
# 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
1.2 初始化ApplicationListener.class
# 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
2.run方法
StopWatch stopWatch = new StopWatch();//构造时间观察器
stopWatch.start();// 打印一个开始时间
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();// 配置headless 依据java.awt.headless
SpringApplicationRunListeners listeners = getRunListeners(args);// 解析如下
// getSpringFactoriesInstances方法获取SpringApplicationRunListener.class对象->EventPublishingRunListener对象
// EventPublishingRunListener对象初始化时,构造器内会执行下面的逻辑
//SimpleApplicationEventMulticaster广播器初始化,并且将listener设置到广播器中
// 将上面获取到的EventPublishingRunListener对象对象添加到SpringApplicationRunListeners对象中
listeners.starting(); // 解析如下
// 调用EventPublishingRunListener对象的starting方法
// 将ApplicationStartingEvent广播出去 SimpleApplicationEventMulticaster->multicastEvent方法
// 调用getApplicationListeners方法获取所有listeners,然后循环广播
// 调用支持事件的listener的listener.onApplicationEvent(event);方法,驱动listener巡行
// 此处支持的listener如下
// org.springframework.boot.logging.LoggingApplicationListener,
// org.springframework.boot.autoconfigure.BackgroundPreinitializer,
// org.springframework.boot.context.config.DelegatingApplicationListener,
// org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
// 创建一个DefaultApplicationArguments对象,构造器中构造了一个新的SOURCE对象
// 根据SOURCE对象的继承结构->SimpleCommandLinePropertySource->parse方法
// 如果args是–开头的,就加入OptionArg中,否则加入到NonOptionArg中
// CommandLinePropertySource构造其中commandLineArgs存入source
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
// 参照下面的prepareEnvironment方法分析
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment); // 打印banner
context = createApplicationContext();
// 创建applicationContext
// 添加AnnotationTypeFilter–>Component 添加AnnotationTypeFilter –> ManagedBean 添加AnnotationTypeFilter –> javax.inject.Named
// exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
// 异常处理
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//
refreshContext(context);
//
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
prepareEnvironment方法分析
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
// 获取或者创建ConfigurableEnvironment 构造environment对象 继承AbstractEnvironment
// AbstractEnvironment构造器中调用customizePropertySources方法
// StandardEnvironment对象:配置systemEnvironment systemProperties
// StandardServletEnvironment对象:配置servletContextInitParams servletConfigInitParams jndiProperties
configureEnvironment(environment, applicationArguments.getSourceArgs());
// 配置环境的信息
// configurePropertySources 配置PropertySources
// commandLineArgs、servletConfigInitParams、servletContextInitParams、jndiProperties(如果存在)、
// systemProperties、systemEnvironment、defaultProperties(如果存在)
// configureProfiles 配置Profiles
// 至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
// application-dev.properties:开发环境 application-test.properties:测试环境 application-prod.properties:生产环境
listeners.environmentPrepared(environment);
// 通知所有的观察者,发送ApplicationEnvironmentPreparedEvent事件.
// org.springframework.boot.context.config.ConfigFileApplicationListener,
// org.springframework.boot.context.config.AnsiOutputApplicationListener,
// org.springframework.boot.logging.LoggingApplicationListener,
// org.springframework.boot.logging.ClasspathLoggingApplicationListener,
// org.springframework.boot.autoconfigure.BackgroundPreinitializer,
// org.springframework.boot.context.config.DelegatingApplicationListener,
// org.springframework.boot.context.FileEncodingApplicationListener
//
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
prepareContext分析
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
// 1. 上下文设置环境
context.setEnvironment(environment);
// 2. 调用postProcessApplicationContext方法设置上下文的beanNameGenerator和resourceLoader(如果SpringApplication有的话)
postProcessApplicationContext(context);
// 3. 拿到之前实例化SpringApplication对象的时候设置的ApplicationContextInitializer,调用它们的initialize方法,对上下文做初始化
applyInitializers(context);
// 4. contextPrepareds 是一个空实现
listeners.contextPrepared(context);
// 5. 打印启动日志
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
} // Add boot specific singleton beans
// 6. 日志往上下文的beanFactory中注册一个singleton的bean,bean的名字是springApplicationArguments,bean的实例是之前实例化的ApplicationArguments对象
context.getBeanFactory().registerSingleton("springApplicationArguments",
applicationArguments);
// 如果之前获取的printedBanner不为空,那么往上下文的beanFactory中注册一个singleton的bean,bean的名字是springBootBanner,bean的实例就是这个printedBanner
if (printedBanner != null) {
context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
} // Load the sources
Set<Object> sources = getSources();
Assert.notEmpty(sources, "Sources must not be empty");
// 7. 调用load方法注册启动类的bean定义,也就是调用SpringApplication.run(Application.class, args);的类,SpringApplication的load方法内会创建BeanDefinitionLoader的对象,并调用它的load()方法
load(context, sources.toArray(new Object[sources.size()]));
// 8. 调用listeners的contextLoaded方法,说明上下文已经加载,该方法先找到所有的ApplicationListener,遍历这些listener,如果该listener继承了ApplicationContextAware类,那么在这一步会调用它的setApplicationContext方法,设置context
listeners.contextLoaded(context);
}
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor
1.SpringFactoriesLoader类
loadFactoryNames->loadSpringFactories->META-INF/spring.factories
位置:spring-boot-2.0.5.RELEASE-sources.jar目录下
springboot 源码笔记的更多相关文章
- SpringBoot是如何实现自动配置的?--SpringBoot源码(四)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 助力SpringBoot自动配置的条件注解ConditionalOnXXX分析--SpringBoot源码(三 ...
- 如何搭建自己的SpringBoot源码调试环境?--SpringBoot源码(一)
1 前言 这是SpringBoot2.1源码分析专题的第一篇文章,主要讲如何来搭建我们的源码阅读调试环境.如果有经验的小伙伴们可以略过此篇文章. 2 环境安装要求 IntelliJ IDEA JDK1 ...
- SpringBoot内置的各种Starter是怎样构建的?--SpringBoot源码(六)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 温故而知新 本篇接 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五) 温 ...
- SpringBoot的启动流程是怎样的?SpringBoot源码(七)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 温故而知新 本篇接 SpringBoot内置的各种Starter是怎样构建的? SpringBoot源码(六) 温故而知新, ...
- SpringApplication对象是如何构建的? SpringBoot源码(八)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 本篇接 SpringBoot的启动流程是怎样的?SpringBoot源码(七) 1 温故而知新 温故而知新,我们来简单回顾一下上 ...
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- SpringBoot内置生命周期事件详解 SpringBoot源码(十)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringBoot事件监听 ...
- Zepto源码笔记(一)
最近在研究Zepto的源码,这是第一篇分析,欢迎大家继续关注,第一次写源码笔记,希望大家多指点指点,第一篇文章由于首次分析原因不会有太多干货,希望后面的文章能成为各位大大心目中的干货. Zepto是一 ...
- redis源码笔记(一) —— 从redis的启动到command的分发
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/redis1 本博客同步在http://www.cnblog ...
随机推荐
- 设计模式之状态模式(State Pattern)
一.什么是状态模式? 把所有动作都封装在状态对象中,状态持有者将行为委托给当前状态对象 也就是说,状态持有者(比如汽车,电视,ATM机都有多个状态)并不知道动作细节,状态持有者只关心自己当前所处的状态 ...
- Javascript/jQuery常用方法
//字符串转成时间 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/ ...
- Kettle 使用入门
Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定. 本文介绍一个入门例子:使用Kettle从动态指定的文件名对应的文本文件里抽取 ...
- 《Python绝技:运用Python成为顶级黑客》 用Python进行渗透测试
1.编写一个端口扫描器 TCP全连接扫描.抓取应用的Banner #!/usr/bin/python #coding=utf-8 import optparse import socket from ...
- Lunix git stash clear 或者 git stash drop后恢复的方法
首先输入 git fsck --lost-found 会看到 一条一条的记录 这里的"dangling commit ..."你可以理解为记录的是你stash的id(经测试,该id ...
- Java 8 LocalDateTime 初使用
LocalTime : 只包括时间 LocalDate : 只包括日期 LocalDateTime : 包括日期和时间 JDBC映射 LocalTime 对应 time LocalDate 对应 d ...
- Android应用安全防护和逆向分析 ——apk反编译
概述 最近一直在学习Android应用安全相关和逆向分析的知识.现在移动app在安全方面是越来越重视了,特别是那些巨头企业涉及到钱的应用,那加密程度,简直是丧心病狂,密密麻麻.从这里可以看出,对于应用 ...
- Linux基本命令学习与使用
1.chgrp,chown,chmod(-R递归修改文件夹下的文件) chgrp:修改文件属于哪个组 chown:修改文件属于哪个用户 chmod:修改文件权限r=4,w=2,x=1 chmod 4+ ...
- linux中进程和计划任务管理
进程和计划任务管理 1. 程序和进程的关系 程序:保存在硬盘.光盘等介质中的可执行代码和数据:静态保存的代码 进程:在 CPU 及内存中运行的程序代码:动态执行的代码:父.子进程:每个进程可以创建一个 ...
- 点击按钮,生成一组一组combobox和slider时,避免控件Id相同,导致控件冲突的方法
如下效果图,点击一次添加按钮,动态生成一组combobox和slider.由于easyUI的下拉框和滑块使用相同的控件id,通过JS生成控件,如果两个id一样就会造成冲突,例如点击第一组的下拉框,第二 ...