package com.lagou;
@SpringBootApplication//标注在类上说明这个类是`SpringBoot`的主配置类
public class SpringBootMytestApplication{
public static void main(String[] args) {
SpringApplication.run(SpringBootMytestApplication.class, args);
}
}
package com.lagou.controller;
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
System.out.println("源码环境构建成功...");
return "源码环境构建成功";
}
}
public class SpringBootMytestApplication{
public static void main(String[] args) {
// 1. 调用 SpringApplication 的 run() 方法
SpringApplication.run(SpringBootMytestApplication.class, args);
}
}
public class SpringApplication {
...
public SpringApplication(Class<?>... primarySources) {
//
this(null, primarySources);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
// 2. 初始化 SpringApplication()
return new SpringApplication(primarySources).run(args);
}
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
// 1. 调用重载方法。其中 primarySource = SpringBootMytestApplication.class
return run(new Class<?>[] { primarySource }, args);
}
}
public class SpringApplication {
...
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
// 设置资源加载器为 null
this.resourceLoader = resourceLoader;
// 断言加载资源类不能为 null
Assert.notNull(primarySources, "PrimarySources must not be null");
// 将 primarySources 数组转换为 List,最后放到 LinkedHashSet 集合中
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 1. 推断应用类型,后面会根据类型初始化对应的环境。常用的一般都是 servlet 环境
this.webApplicationType = WebApplicationType.deduceFromClasspath();
...
}
}
public enum WebApplicationType {
NONE,
SERVLET,
REACTIVE;
private static final String WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
private static final String[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet",
"org.springframework.web.context.ConfigurableWebApplicationContext" };
...
static WebApplicationType deduceFromClasspath() {
// 1. classpath 下必须存在 org.springframework.web.reactive.DispatcherHandler
if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
for (String className : SERVLET_INDICATOR_CLASSES) {
// 2. classpath 环境下不存在 javax.servlet.Servlet 或者 org.springframework.web.context.ConfigurableWebApplicationContext
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}
return WebApplicationType.SERVLET;
}
}
public class SpringApplication {
...
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
...
// 1. 初始化 classpath 下 META-INF/spring.factories 中已配置的 ApplicationContextInitializer
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
...
}
public void setInitializers(Collection<? extends ApplicationContextInitializer<?>> initializers) {
// 2. 设置参数
this.initializers = new ArrayList<>(initializers);
}
}
public class SpringApplication {
...
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
return getSpringFactoriesInstances(type, new Class<?>[] {});
}
/**
* 通过指定的 classloader 从 META-INF/spring.factories 获取指定的 Spring 的工厂实例
*/
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = getClassLoader();
// 1. 通过指定的 classLoader 从 META-INF/spring.factories 的资源文件中,
// 读取 key 为 type.getName() 的 value
Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
// 2. 创建工厂实例
List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
// 4. 对 Spring 工厂实例排序(org.springframework.core.annotation.Order 注解指定的顺序)
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
ClassLoader classLoader, Object[] args, Set<String> names) {// 反射创建对象
List<T> instances = new ArrayList<>(names.size());
for (String name : names) {
try {
Class<?> instanceClass = ClassUtils.forName(name, classLoader);
Assert.isAssignable(type, instanceClass);
Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
// 3. 对象实例化
T instance = (T) BeanUtils.instantiateClass(constructor, args);
instances.add(instance);
}
catch (Throwable ex) {
throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
}
}
return instances;
}
}
5. 设置完初始化器之后,开始设置监听器。同样是调用 getSpringFactoriesInstances() 的工作流程,首先去 META-INF/spring.factories 找到所有 key 为 org.springframework.context.ApplicationListener 的实现类,逐一初始化,并按照 org.springframework.core.annotation.Order 注解排序
public class SpringApplication {
...
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
...
// 1. 初始化 classpath 下 META-INF/spring.factories 中已配置的 ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
...
}
public void setListeners(Collection<? extends ApplicationListener<?>> listeners) {
// 2. 设置参数
this.listeners = new ArrayList<>(listeners);
}
}
6. 然后根据调用栈,找到 main 入口,获取到主程序类,设置到 mainApplicationClass 属性中,并返回
public class SpringApplication {
...
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
...
// 1. 根据调用栈,推断出 main 方法的类名
this.mainApplicationClass = 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;
}
}
public class SpringApplication {
...
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
// 两件事:
// 初始化 SpringApplication
// 1. 执行 run() 方法
return new SpringApplication(primarySources).run(args);
}
}
public class SpringApplication {
...
public ConfigurableApplicationContext run(String... args) {
// 记录程序运行时间
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// ConfigurableApplicationContext Spring 的上下文
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
// 1. 获取并动监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
...
}
}
public class SpringApplication {
private final Log log;
private final List<SpringApplicationRunListener> listeners;
...
private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
// 1. SpringApplicationRunListeners 负责在 SpringBoot 启动的不同阶段,
// 广播出不同的消息,传递给 ApplicationListener 监听器实现类
return new SpringApplicationRunListeners(logger,
getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}
}
class SpringApplicationRunListeners {
private final Log log;
private final List<SpringApplicationRunListener> listeners;
...
SpringApplicationRunListeners(Log log, Collection<? extends SpringApplicationRunListener> listeners) {
this.log = log;
this.listeners = new ArrayList<>(listeners);
}
}
public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {
private final SpringApplication application;
private final String[] args;
private final SimpleApplicationEventMulticaster initialMulticaster;
public EventPublishingRunListener(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
this.initialMulticaster = new SimpleApplicationEventMulticaster();
for (ApplicationListener<?> listener : application.getListeners()) {
this.initialMulticaster.addApplicationListener(listener);
}
}
...
}
public class SpringApplication {
...
public ConfigurableApplicationContext run(String... args) {
...
// 获取并启动监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
// 1. 给 listener 发送 starting 事件回调
listeners.starting();
...
}
}
class SpringApplicationRunListeners {
private final Log log;
private final List<SpringApplicationRunListener> listeners;
...
// 1. 在 run() 方法开始执行时,该方法就立即被调用,可用于在初始化最早期时做一些工作
void starting() {
for (SpringApplicationRunListener listener : this.listeners) {
listener.starting();
}
}
}
public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {
...
@Override
public void starting() {
this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
}
}
server.port=8080
server.port=2222
--spring.profiles.active=prod
public class SpringApplication {
...
public ConfigurableApplicationContext run(String... args) {
...
try {
// 将运行时参数封装
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 1. 构造应用上下文环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// 2. 处理需要忽略的 Bean
configureIgnoreBeanInfo(environment);
// 3. 打印 banner
Banner printedBanner = printBanner(environment);
...
}
catch (Throwable ex) {
...
}
...
}
}
public class SpringApplication {
...
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {// 构建整个环境上下文(用户配置,profile)
// 1. 创建并配置相应的环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
...
}
}
public class SpringApplication {
private WebApplicationType webApplicationType;
...
private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
switch (this.webApplicationType) {
case SERVLET:
return StandardServletEnvironment.class;
case REACTIVE:
return StandardReactiveWebEnvironment.class;
default:
return StandardEnvironment.class;
}
}
}
public class SpringApplication {
...
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {// 构建整个环境上下文(用户配置,profile)
// 创建并配置相应的环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
// 1. 根据用户配置,配置 environment 系统环境
configureEnvironment(environment, applicationArguments.getSourceArgs());
...
}
}
--spring.profiles.active=prod
运行时参数封装成 SimpleCommandLinePropertySource 加入环境中public class SpringApplication {
...
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
if (this.addConversionService) {
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
environment.setConversionService((ConfigurableConversionService) conversionService);
}
// 1. 将 main() 的 args(即 --spring.profiles.active=prod)封装成 SimpleCommandLinePropertySource 加入环境中
configurePropertySources(environment, args);
...
}
}
public class SpringApplication {
...
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
// 1. 先获取 MutablePropertySources
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
}
if (this.addCommandLineProperties && args.length > 0) {
// 2. 判断是否包含名为 commandLineArgs 的属性
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) {
...
}
else {
// 3. 把运行时参数(即 --spring.profiles.active=prod)封装成 SimpleCommandLinePropertySource 添加到源中
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
}
}
}
public class SpringApplication {
...
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
if (this.addConversionService) {
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
environment.setConversionService((ConfigurableConversionService) conversionService);
}
// 将 main() 的 args(即 --spring.profiles.active=prod)封装成 SimpleCommandLinePropertySource 加入环境中
configurePropertySources(environment, args);
// 1. 激活相应的配置文件
configureProfiles(environment, args);
}
}
public class SpringApplication {
private Set<String> additionalProfiles = new HashSet<>();
...
protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {// 设置激活的profile信息
Set<String> profiles = new LinkedHashSet<>(this.additionalProfiles);
// 1. 获取到的 activeProfiles 就是 prod
profiles.addAll(Arrays.asList(environment.getActiveProfiles()));
// 2. 把对应属性设置为 prod
environment.setActiveProfiles(StringUtils.toStringArray(profiles));
}
}
public class SpringApplication {
...
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {// 构建整个环境上下文(用户配置,profile)
// 创建并配置相应的环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
// 根据用户配置,配置 environment系统环境
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
// 1. 启动相应的监听器,其中一个重要的监听器 ConfigFileApplicationListener 就是加载项目配置文件的监听器。
listeners.environmentPrepared(environment);
...
}
}
public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {
private final SpringApplication application;
private final String[] args;
private final SimpleApplicationEventMulticaster initialMulticaster;
...
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
// 1. 通过广播器进行广播发布
this.initialMulticaster
.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
}
}
public class SimpleApplicationEventMulticaster extends AbstractApplicationEventMulticaster {
@Nullable
private Executor taskExecutor;
...
@Override
public void multicastEvent(ApplicationEvent event) {
multicastEvent(event, resolveDefaultEventType(event));
}
...
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
// 1. 解析 ApplicationEvent 类型,这里是 org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
// 2. 线程池为空,所以不会调用线程池来执行
Executor executor = getTaskExecutor();
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
// 3. 对 ApplicationListener 执行监听任务
invokeListener(listener, event);
}
}
}
}
public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
...
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
// 1. 加载项目配置文件
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}
}
public class SpringApplication {
...
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {// 构建整个环境上下文(用户配置,profile)
// 创建并配置相应的环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
// 根据用户配置,配置 environment系统环境
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
// 启动相应的监听器,其中一个重要的监听器 ConfigFileApplicationListener 就是加载项目配置文件的监听器。
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
// 1. 返回环境
return environment;
}
}
public class SpringApplication {
...
public ConfigurableApplicationContext run(String... args) {
...
try {
// 将运行时参数封装
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 构造应用上下文环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// 处理需要忽略的 Bean
configureIgnoreBeanInfo(environment);
// 打印 banner
Banner printedBanner = printBanner(environment);
// 1. 刷新应用上下文前的准备阶段
context = createApplicationContext();
...
}
catch (Throwable ex) {
...
}
...
}
}
public class SpringApplication {
private WebApplicationType webApplicationType;
private Class<? extends ConfigurableApplicationContext> applicationContextClass;
public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context."
+ "annotation.AnnotationConfigApplicationContext";
public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."
+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
...
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
}
public class SpringApplication {
...
public ConfigurableApplicationContext run(String... args) {
...
try {
// 将运行时参数封装
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 构造应用上下文环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// 处理需要忽略的 Bean
configureIgnoreBeanInfo(environment);
// 打印 banner
Banner printedBanner = printBanner(environment);
// 刷新应用上下文前的准备阶段
context = createApplicationContext();
// 1. 实例化 SpringBootExceptionReporter.class,用来支持报告关于启动的错误
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
...
}
catch (Throwable ex) {
...
}
...
}
}