springboot2.1.7启动分析(一)SpringApplication实例化

	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}
  • webApplicationType :servlet
  • getSpringFactoriesInstances :获取指定类型的springFactory实例。通过查找META-INF/spring.factories文件,将所有的键值对存到LinkedMultiValueMap中,且做了缓存处理。然后从该map中读取指定key的value值。通过查看                                                           spring.factories文件可知,所有value的每一项值都是类的全路径名,所以接下来就是通过反射实例化出来这些对象。
  • setInitializers、setListeners:将getSpringFactoriesInstances获取到的对应的实例化对象集合初始化到对应的变量上
  • deduceMainApplicationClass:从调用的堆栈跟踪中的元素中获取到方法名为main的类的calss

从代码可以看出ApplicationContextInitializer和ApplicationListenerduiding实例化时调用的是无参的构造函数(Class<?>[] parameterTypes, Object... args)为空数组和空对象。

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
		return getSpringFactoriesInstances(type, new Class<?>[] {});
	}
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
	}
//通过classLoader获取"META-INF/spring.factories",且做了缓存处理。通过代码可以看出,spring.factories文件和properties文件处理方式是一样的
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);
		}
	}

附:ApplicationContextInitializer 实例 initializers、ApplicationListener 实例  listeners

猜你喜欢

转载自blog.csdn.net/sinat_33472737/article/details/112756918