SpringBoot源码分析(SpringApplication构造)

在这里插入图片描述
随后会进入SpringApplication的构造

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	this.resourceLoader = resourceLoader;
	Assert.notNull(primarySources, "PrimarySources must not be null");
	// primarySources指的是Spring Boot的启动类
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
	// 判断当前环境 WebApplicationType.NONE SERVLET REACTIVE
	// 1. org.springframework.web.reactive.DispatcherHandler而且不存在org.springframework.web.servlet.DispatcherServlet-->REACTIVE
	// 2. 不存在org.springframework.web.context.ConfigurableWebApplicationContext或javax.servlet.Servlet有一个不存在-->NONE
	// 3. 其他情况 --> SERVLET 
	this.webApplicationType = WebApplicationType.deduceFromClasspath();
	// 重点: SPI模式 设置ApplicationContextInitializer类型的应用上下文启动类
	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
	// 同理 设置ApplicationListener类型的监听器
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
	// 判断主类
	this.mainApplicationClass = deduceMainApplicationClass();
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以下为加载spring.factory文件的源码:

// 当前类:org.springframework.core.io.support.SpringFactoriesLoader
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
	MultiValueMap<String, String> result = cache.get(classLoader);
	if (result != null) {
		return result;
	}

	try {
		Enumeration<URL> urls = (classLoader != null ?
				classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
				ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
		result = new LinkedMultiValueMap<>();
		while (urls.hasMoreElements()) {
			URL url = urls.nextElement();
			UrlResource resource = new UrlResource(url);
			Properties properties = PropertiesLoaderUtils.loadProperties(resource);
			for (Map.Entry<?, ?> entry : properties.entrySet()) {
				String factoryClassName = ((String) entry.getKey()).trim();
				for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
					result.add(factoryClassName, factoryName.trim());
				}
			}
		}
		cache.put(classLoader, result);
		return result;
	}
	catch (IOException ex) {
		throw new IllegalArgumentException("Unable to load factories from location [" +
				FACTORIES_RESOURCE_LOCATION + "]", ex);
	}
}

General purpose factory loading mechanism for internal use within the framework.
SpringFactoriesLoader loads and instantiates factories of a given type from “META-INF/spring.factories” files which may be present in multiple JAR files in the classpath. The spring.factories file must be in Properties format, where the key is the fully qualified name of the interface or abstract class, and the value is a comma-separated list of implementation class names. For example:
example.MyService=example.MyServiceImpl1,example.MyServiceImpl2
where example.MyService is the name of the interface, and MyServiceImpl1 and MyServiceImpl2 are two implementations.

以上就是SpringApplication初始化的逻辑,其中主要就是判断当前环境的WebApplicationType、ApplicationContextInitializer、ApplicationListener还有主类,其中获取应用上下文初始类和应用监听器用到SPI模式。

发布了34 篇原创文章 · 获赞 1 · 访问量 1534

猜你喜欢

转载自blog.csdn.net/m0_37607945/article/details/104937841