『SpringBoot 源码分析』run() 方法执行流程:(1)初始化 SpringApplication 、上下文环境、应用上下文

news2024/9/24 13:19:17

『SpringBoot 源码分析』run() 方法执行流程:(1)初始化 SpringApplication 、上下文环境、应用上下文

  • 基于 2.2.9.RELEASE
  • 问题:当方法进行了注释标记之后,springboot 又是怎么注入到容器中并创建类呢?
  1. 首先创建测试主程序
package com.lagou;

@SpringBootApplication//标注在类上说明这个类是`SpringBoot`的主配置类
public class SpringBootMytestApplication{

	public static void main(String[] args) {
		SpringApplication.run(SpringBootMytestApplication.class, args);
	}
}
  1. 创建测试 Controller
package com.lagou.controller;

@RestController
public class TestController {

	@RequestMapping("/test")
	public String test(){
		System.out.println("源码环境构建成功...");
		return "源码环境构建成功";
	}
}

SpringApplication 初始化过程

  1. SpringApplication 的初始化过程就是从 run() 开始的
public class SpringBootMytestApplication{

	public static void main(String[] args) {
		// 1. 调用 SpringApplication 的 run() 方法
		SpringApplication.run(SpringBootMytestApplication.class, args);
	}
}
  1. 在真正执行 run() 方法之前,首先需要初始化 SpringApplication()
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);
	}
}
  1. 执行 SpringApplication() 初始化时,首先设置资源加载器为 null,同时将 primarySources 转换为 List 存到属性中,然后开始推断应用启动的类型
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();
		...
	}
}
  • 推断应用启动类型的时候,首先判断 classpath 下面是否存在 org.springframework.web.reactive.DispatcherHandler,如果存在,则把应用设置为 REACTIVE 类型。如果不存在,则继续判断 classpath 下面是否存在 javax.servlet.Servlet 或者 org.springframework.web.context.ConfigurableWebApplicationContext,不存在就设置应用类型为 None,否则默认是 SERVLET
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;
	}
}
  1. 推断完程序的应用类型之后吗,就开始设置初始化器
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);
	}
}
  • 其中,初始化器的设置主要思路是从 META-INF/spring.factories 找到所有 key 为 org.springframework.context.ApplicationContextInitializer 的实现类,然后逐一初始化,并按照 org.springframework.core.annotation.Order 注解排序
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;
	}
}
  1. 总结
    在这里插入图片描述

获取启动监听器

  1. 当完成初始化 SpringApplication 后,开始真正执行 run() 方法
public class SpringApplication {
	...
	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		// 两件事:
		// 初始化 SpringApplication
		// 1. 执行 run() 方法
		return new SpringApplication(primarySources).run(args);
	}
}
  1. 首先初始化 StopWatch 监控程序运行时间,然后开始获取运行时监听器
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);
		...
	}
}
  1. 在执行 getRunListeners() 时,首先同样需要先从 META-INF/spring.factories 获取 key 为 org.springframework.boot.SpringApplicationRunListener 的实现类,然后存到属性中。其中 getSpringFactoriesInstances() 已经说明过了,就不再叙述,同时 SpringApplicationRunListener.classorg.springframework.context.ApplicationListener 不一样,这个是个发布器,主要是将在 SpringBoot 不同阶段广播出来的消息传递给 ApplicationListener 实现类
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));
	}
}

在这里插入图片描述

  • 其中,SpringApplicationRunListeners 对所有 SpringApplicationRunListener.class 的实现类进行了封装
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);
	}
}
  • 其实实现了 SpringApplicationRunListener.class 接口的,也就只有 org.springframework.boot.context.event.EventPublishingRunListener,它初始化的时候,会把所有 org.springframework.context.ApplicationListener 存入到属性中,用于后面的广播事件给这些监听器
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);
		}
	}
	...
}
  1. 当拿到运行时监听器之后,就开始对每个运行时监听器执行监听
public class SpringApplication {
	...
	public ConfigurableApplicationContext run(String... args) {
		...
		// 获取并启动监听器
		SpringApplicationRunListeners listeners = getRunListeners(args);
		// 1. 给 listener 发送 starting 事件回调
		listeners.starting();
		...
	}
}
  1. 其中,所有的运行时监听器都封装在 SpringApplicationRunListeners,实际也就只有一个运行时监听器 org.springframework.boot.context.event.EventPublishingRunListener
class SpringApplicationRunListeners {

	private final Log log;
	private final List<SpringApplicationRunListener> listeners;
	...
	// 1. 在 run() 方法开始执行时,该方法就立即被调用,可用于在初始化最早期时做一些工作
	void starting() {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.starting();
		}
	}
}
  1. 在执行 starting() 的时候,实际上就是给所有 org.springframework.context.ApplicationListener 发布一个 ApplicationStartingEvent 事件
public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {
	...
	@Override
	public void starting() {
		this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
	}
}
  1. 总结
    在这里插入图片描述

构建应用上下文环境(上)

  1. 首先在程序启动前配置两个文件,并且启动时添加运行时参数
  • 设置 application.properties
server.port=8080
  • 设置 application-prod.properties
server.port=2222
  • 运行时参数设置
--spring.profiles.active=prod
  1. 当完成监听器配置,并启动监听后,就需要设置应用环境上下文,从而为其他配置提供一个统一配置环境
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) {
			...
		}
		...
	}
}
  1. 首先执行 prepareEnvironment(),调用 getOrCreateEnvironment() 配置相应的运行时环境
public class SpringApplication {
	...
	private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
			ApplicationArguments applicationArguments) {// 构建整个环境上下文(用户配置,profile)
		// 1. 创建并配置相应的环境
		ConfigurableEnvironment environment = getOrCreateEnvironment();
		...
	}
}
  • 其中 getOrCreateEnvironment() 是根据之前创建 SpringApplication 时推断出来的 webApplicationType 来决定创建哪个环境,这里肯定创建的是 StandardServletEnvironment
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;
		}
	}
}
  1. 完成创建 StandardServletEnvironment 之后,需要根据用户配置配置 environment 系统环境
public class SpringApplication {
	...
	private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
			ApplicationArguments applicationArguments) {// 构建整个环境上下文(用户配置,profile)
		// 创建并配置相应的环境
		ConfigurableEnvironment environment = getOrCreateEnvironment();
		// 1. 根据用户配置,配置 environment 系统环境
		configureEnvironment(environment, applicationArguments.getSourceArgs());
		...
	}
}
  • 配置 environment 系统环境时,首先把刚刚加的 --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);
		...
	}
}
  • 其中加入参数时,先从 environment 中获取 MutablePropertySources 源,然后判断是否包含名为 commandLineArgs 的属性,不包含的话,就把运行时参数封装成 SimpleCommandLinePropertySource 添加到源中
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));
			}
		}
	}
}
  • 配置完 environment 系统环境后,就开始激活环境
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);
	}
}
  • 激活的时候,就是把 environment 的属性开启为对应激活的配置,这里就是 prod
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));
	}
}
  1. 配置环境并激活后,就需要使用监听器发布对应的环境给对应的监听器
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);
		...
	}
}
  • 其中,监听器的发布,交由 org.springframework.boot.context.event.EventPublishingRunListener 广播给所有的 org.springframework.context.ApplicationListener,其中,EventPublishingRunListener 会把 environment 封装成 ApplicationEnvironmentPreparedEvent,然后调用广播器广播
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));
	}
}
  • 广播器实际上就是先解析 ApplicationEvent 的类型,然后获取对应的 ApplicationListener 来执行监听任务
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);
			}
		}
	}
}
  • 其中,最重要的是 ConfigFileApplicationListener 监听器,主要是用来加载项目配置文件
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);
		}
	}
}
  1. 完成系统环境预处理之后,就将环境配置返回给上文使用
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;
	}
}
  1. 总结
    在这里插入图片描述

构建应用上下文环境(下)

  1. 完成上下文环境初始化后,第一步开始构建应用上下文
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) {
			...
		}
		...
	}
}
  • 创建上下文也是根据 SpringApplication 初始化时的应用类型来初始化对应应用上下文,这次的类型是 SERVLET,所以创建的时候会初始化 org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext。创建是交由 BeanUtils.instantiateClass 创建
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);
	}
}
  • 其中,AnnotationConfigServletWebServerApplicationContext 实现了 ConfigurableWebServerApplicationContext 接口,所以返回的时候,能够强转返回。同时,也继承了 GenericWebApplicationContext,后面的代码中会经常看到这个类
    在这里插入图片描述
    • beanFactory 正是 GenericWebApplicationContext 中定义的,在 BeanUtils.instantiateClass 时,不但初始化了我们的上下文(AnnotationConfigServletWebServerApplicationContext),而且触发了 GenericWebApplicationContext 的构造函数,所以 IOC 容器也创建了
      在这里插入图片描述
  1. 创建完 AnnotationConfigServletWebServerApplicationContext 上下文之后,再从 META-INF/spring.factories 找到所有 key 为 org.springframework.boot.SpringBootExceptionReporter 实现类,用于报告启动的错误
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) {
			...
		}
		...
	}
}
  1. 总结
    在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/900486.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java入门必备|有你想知道的代码技巧

前言 本文主要分享记录学习Java时的敲代码大法&#xff0c;一步步与idea这个软件磨合&#xff0c;让它为我们敲代码这条路提供更便捷的帮助&#xff08;雀食好用哈&#xff09; 一.psvm 很多刚上手IJ软件&#xff0c;就被main()方法给折服了&#xff0c;这段代码量十分大 当…

常见指令以及权限理解

常见指令以及权限理解 命令格式&#xff1a; command [-options] parameter1 parameter1 命令 选项 参数1 参数2 1.command为命令名称&#xff0c;例如变化目录的cd等 2.中括号[ ]实际在命令中是不存在的&#xff0c;这个中括号代表可选&#xff0c;通常选项前面会添加一个符号…

Linux命令200例:clock的具体应用,设置系统的时钟时间、硬件时钟和定时器等相关信息

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;全栈领域新星创作者✌。CSDN专家博主&#xff0c;阿里云社区专家博主&#xff0c;2023年6月csdn上海赛道top4。 &#x1f3c6;数年电商行业从业经验&#xff0c;历任核心研发工程师&#xff0c;项目技术负责人。 &…

缺少或找不到vcruntime140_1.dll的解决方法

某天&#xff0c;当我准备打开电脑上的一个应用程序时&#xff0c;突然收到一个错误提示&#xff0c;显示缺少了vcruntime140_1.dll文件。这个文件是一个重要的系统组件&#xff0c;它的丢失导致了我无法正常运行该应用程序。于是&#xff0c;我开始了一场寻找和修复旅程。然而…

“深度学习”学习日记:Tensorflow实现VGG每一个卷积层的可视化

2023.8.19 深度学习的卷积对于初学者是非常抽象&#xff0c;当时在入门学习的时候直接劝退一大班人&#xff0c;还好我坚持了下来。可视化时用到的图片&#xff08;我们学校的一角&#xff01;&#xff01;&#xff01;&#xff09;以下展示了一个卷积和一次Relu的变化 作者使…

leetcode 387.字符串中第一个唯一字符

⭐️ 题目描述 &#x1f31f; leetcode链接&#xff1a;https://leetcode.cn/problems/first-unique-character-in-a-string/description/ 思路&#xff1a; 比较优的方式使用相对映射记录的方式。在 ASCII 表中小写字母 -97 就是 0 - 25。在依次从前遍历查找即可。需要注意的…

论文笔记:Continuous Trajectory Generation Based on Two-Stage GAN

2023 AAAI 1 intro 1.1 背景 建模人类个体移动模式并生成接近真实的轨迹在许多应用中至关重要 1&#xff09;生成轨迹方法能够为城市规划、流行病传播分析和交通管控等城市假设分析场景提供仿仿真数据支撑2&#xff09;生成轨迹方法也是目前促进轨迹数据开源共享与解决轨迹数…

十六、Spring Cloud Sleuth 分布式请求链路追踪

目录 一、概述1、为什么出出现这个技术&#xff1f;需要解决哪些问题2、是什么&#xff1f;3、解决 二、搭建链路监控步骤1、下载运行zipkin2、服务提供者3、服务调用者4、测试 一、概述 1、为什么出出现这个技术&#xff1f;需要解决哪些问题 2、是什么&#xff1f; 官网&am…

RingBuffer 环形缓冲区----镜像指示位

文字和图片参考和来自这些文章&#xff1a; 大疆嵌入式软件编程题找鞍点_已知循环缓冲区是一个可以无限循环读写的缓冲区,当缓冲区满了还继续写的话就会覆_一禅的师兄的博客-CSDN博客 ring buffer&#xff0c;一篇文章讲透它&#xff1f; - 知乎 (zhihu.com) 1 概述 1.1 什…

Python可视化在量化交易中的应用(15)_Seaborn箱线图小提琴图

Seaborn中箱线图和小提琴图的绘制方法 箱线图和小提琴图常被用来观测数据的中位数、上下四分位数分布范围以及异常值的分布情况。 seaborn中绘制箱线图使用的是sns.boxplot()函数。 sns.boxplot(x,y,hue,data,order,hue_order,orient,color,palette,saturation0.75,width0.8,do…

C语言 功能型API --------------------strcat()

NAME strcat, strncat - concatenate two strings 头文件 SYNOPSIS #include <string.h> 函数原型&#xff1a; char *strcat(char *dest, const char *src); 功能&#xff1a; 在字符串dest的末尾将字符串src拼接上去 #include <stdio.h> #inc…

227、仿真-基于51单片机锅炉热电偶PT100铂电阻温度控制Proteus仿真设计(程序+Proteus仿真+原理图+流程图+元器件清单+配套资料等)

毕设帮助、开题指导、技术解答(有偿)见文未 目录 一、设计功能 二、Proteus仿真图 三、原理图 四、程序源码 资料包括&#xff1a; 需要完整的资料可以点击下面的名片加下我&#xff0c;找我要资源压缩包的百度网盘下载地址及提取码。 方案选择 单片机的选择 方案一&…

LlamaGPT -基于Llama 2的自托管类chatgpt聊天机器人

LlamaGPT一个自托管、离线、类似 ChatGPT 的聊天机器人&#xff0c;由 Llama 2 提供支持。100% 私密&#xff0c;不会有任何数据离开你的设备。 推荐&#xff1a;用 NSDT编辑器 快速搭建可编程3D场景 1、如何安装LlamaGPT LlamaGPT可以安装在任何x86或arm64系统上。 首先确保…

网络编程(TCP和UDP的基础模型)

一、TCP基础模型&#xff1a; tcp Ser&#xff1a; #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <head.h>#define PORT 88…

探索无限创造力的星辰大道,画出想象的浩瀚宇宙!-turtle

介绍 视频教程地址在此&#xff1a;https://www.bilibili.com/video/BV1Pm4y1H7Tb/ 大家好&#xff0c;欢迎来到本视频&#xff01;今天&#xff0c;我们将一同探索Python编程世界中的一个有趣而创意的库——Turtle库。无需专业绘画技能&#xff0c;你就可以轻松地用代码绘制…

docker的安装与基础使用

一.docker简介 1&#xff09;什么是docker Docker是一种用于构建、打包和运行应用程序的开源平台。它基于操作系统级虚拟化技术&#xff0c;可以将应用程序和其依赖的库、环境等资源打包到一个可移植的容器中&#xff0c;形成一个轻量级、独立的可执行单元。 开发者在本地编…

QT TLS initialization failed问题(已解决) QT基础入门【网络编程】openssl

问题: qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed 这个问题的出现主要是使用了https请求:HTTPS ≈ HTTP + SSL,即有了加密层的HTTP 所以Qt 组件库需要OpenSSL dll 文件支持HTTPS 解决: 1.加入以下两行代码获取QT是否支持opensll以…

【学会动态规划】单词拆分(24)

目录 动态规划怎么学&#xff1f; 1. 题目解析 2. 算法原理 1. 状态表示 2. 状态转移方程 3. 初始化 4. 填表顺序 5. 返回值 3. 代码编写 写在最后&#xff1a; 动态规划怎么学&#xff1f; 学习一个算法没有捷径&#xff0c;更何况是学习动态规划&#xff0c; 跟我…

向gitee推送代码

目录 一、Gitee创建仓库 二、将刚刚创建的仓库放到虚拟机上 2.1 https 方式克隆仓库 2.2 ssh的方式克隆仓库 三、本地开发&#xff0c;推送 3.1 查看是否有远程库 3.2 推送代码 3.3 查看是否推送成功 一、Gitee创建仓库 二、将刚刚创建的仓库放到虚拟机上 2.1 https 方式…

codesys和HMI通讯

codesys可视化有2种&#xff1a; 网页web // 类似于路由器管理那样&#xff0c;登录网页就能操作 本地HMI // 其他品牌的触摸屏 符号配置&#xff1a; 1 编译需要的变量 2 导出XML文件 3 触摸屏软件加载XML文件